Convert double to BigDecimal and set BigDecimal Precision
Asked Answered
U

9

105

In Java, I want to take a double value and convert it to a BigDecimal and print out its String value to a certain precision.

import java.math.BigDecimal;
public class Main {
    public static void main(String[] args) {
        double d=-.00012;
        System.out.println(d+""); //This prints -1.2E-4

        double c=47.48000;
        BigDecimal b = new BigDecimal(c);
        System.out.println(b.toString()); 
        //This prints 47.47999999999999687361196265555918216705322265625 
    }
}

It prints this huge thing:

47.47999999999999687361196265555918216705322265625

and not

47.48

The reason I'm doing the BigDecimal conversion is sometimes the double value will contain a lot of decimal places (i.e. -.000012) and the when converting the double to a String will produce scientific notation -1.2E-4. I want to store the String value in non-scientific notation.

I want to have BigDecimal always have two units of precision like this: "47.48". Can BigDecimal restrict precision on conversion to string?

Ullage answered 12/9, 2012 at 19:50 Comment(1)
Note: BigDecimal will also sometimes use the scientific notation (I believe for number < 10^-6)Separable
W
69

It prints 47.48000 if you use another MathContext:

BigDecimal b = new BigDecimal(d, MathContext.DECIMAL64);

Just pick the context you need.

Wallet answered 12/9, 2012 at 19:57 Comment(5)
Using the double constructor is not recommended.Separable
#1057222Beatification
BigDecimal b = BigDecimal.valueOf(d);Whatsoever
@ rvazquezglez like your solutionOuthouse
Still flawed for other values. For example new BigDecimal( 1E-11, MathContext.DECIMAL64).toPlainString() prints 0.000000000009999999999999999, but BigDecimal.valueOf works as expected.Outrelief
B
167

The reason of such behaviour is that the string that is printed is the exact value - probably not what you expected, but that's the real value stored in memory - it's just a limitation of floating point representation.

According to javadoc, BigDecimal(double val) constructor behaviour can be unexpected if you don't take into consideration this limitation:

The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.

So in your case, instead of using

double val = 77.48;
new BigDecimal(val);

use

BigDecimal.valueOf(val);

Value that is returned by BigDecimal.valueOf is equal to that resulting from invocation of Double.toString(double).

Beatification answered 27/5, 2013 at 13:24 Comment(3)
Ran into the exact same problem today and this is IMHO the most suitable answer! Thanks!Sicular
I suggest to add .stripTrailingZeros() after BigDecimal.valueOf(val) to ommit trailling zeros. exemple: BigDecimal.valueOf(15.0).stripTrailingZeros(); -> 15 (instead of 15.0 if you don't call .stripTrailingZeros()Renaldo
This should be the accepted answer.Simons
W
69

It prints 47.48000 if you use another MathContext:

BigDecimal b = new BigDecimal(d, MathContext.DECIMAL64);

Just pick the context you need.

Wallet answered 12/9, 2012 at 19:57 Comment(5)
Using the double constructor is not recommended.Separable
#1057222Beatification
BigDecimal b = BigDecimal.valueOf(d);Whatsoever
@ rvazquezglez like your solutionOuthouse
Still flawed for other values. For example new BigDecimal( 1E-11, MathContext.DECIMAL64).toPlainString() prints 0.000000000009999999999999999, but BigDecimal.valueOf works as expected.Outrelief
K
20

You want to try String.format("%f", d), which will print your double in decimal notation. Don't use BigDecimal at all.

Regarding the precision issue: You are first storing 47.48 in the double c, then making a new BigDecimal from that double. The loss of precision is in assigning to c. You could do

BigDecimal b = new BigDecimal("47.48")

to avoid losing any precision.

Keon answered 12/9, 2012 at 20:0 Comment(0)
F
11

Why not :

b = b.setScale(2, RoundingMode.HALF_UP);
Fetich answered 20/10, 2016 at 7:21 Comment(1)
why not BigDecimal b = new BigDecimal(c).setScale(2,BigDecimal.ROUND_HALF_UP);Floorer
C
6

It's printing out the actual, exact value of the double.

Double.toString(), which converts doubles to Strings, does not print the exact decimal value of the input -- if x is your double value, it prints out exactly enough digits that x is the closest double to the value it printed.

The point is that there is no such double as 47.48 exactly. Doubles store values as binary fractions, not as decimals, so it can't store exact decimal values. (That's what BigDecimal is for!)

Culm answered 12/9, 2012 at 20:7 Comment(0)
B
5

In Java 9 the following is deprecated:

BigDecimal.valueOf(d).setScale(2, BigDecimal.ROUND_HALF_UP);

instead use:

BigDecimal.valueOf(d).setScale(2, RoundingMode.HALF_UP);

Example:

    double d = 47.48111;

    System.out.println(BigDecimal.valueOf(d)); //Prints: 47.48111

    BigDecimal bigDecimal = BigDecimal.valueOf(d).setScale(2, RoundingMode.HALF_UP);
    System.out.println(bigDecimal); //Prints: 47.48
Backward answered 1/12, 2017 at 13:59 Comment(0)
A
3

The String.format syntax helps us convert doubles and BigDecimals to strings of whatever precision.

This java code:

double dennis = 0.00000008880000d;
System.out.println(dennis);
System.out.println(String.format("%.7f", dennis));
System.out.println(String.format("%.9f", new BigDecimal(dennis)));
System.out.println(String.format("%.19f", new BigDecimal(dennis)));

Prints:

8.88E-8
0.0000001
0.000000089
0.0000000888000000000
Arapaima answered 7/3, 2013 at 7:50 Comment(0)
N
2
BigDecimal b = new BigDecimal(c).setScale(2,BigDecimal.ROUND_HALF_UP);
Nessy answered 5/5, 2017 at 2:18 Comment(0)
G
0

Wrap using BigDecimal.valueOf():

double d = 47.48111;
BigDecimal.valueOf(d)
Gosse answered 26/2, 2023 at 3:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.