Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigDecimal to string

I have a BigDecimal object and i want to convert it to string. The problem is that my value got fraction and i get a huge number (in length) and i only need the original number in string for example: for

BigDecimal bd = new BigDecimal(10.0001) System.out.println(bd.toString()); System.out.println(bd.toPlainString()); 

the output is:

10.000099999999999766941982670687139034271240234375 10.000099999999999766941982670687139034271240234375 

and i need the out put to be exactly the number 10.0001 in string

like image 983
Yo Al Avatar asked Dec 16 '12 09:12

Yo Al


People also ask

How do I convert BigDecimal to integer?

intValue()converts this BigDecimal to an int. This conversion is analogous to the narrowing primitive conversion from double to short. Any fractional part of this BigDecimal will be discarded, and if the resulting "BigInteger" is too big to fit in an int, only the low-order 32 bits are returned.

How does String compare to BigDecimal?

BigDecimal. compareTo(BigDecimal val) compares the BigDecimal Object with the specified BigDecimal value. Two BigDecimal objects that are equal in value but have a different scale (like 2.0 and 2.00) are considered equal by this method.

How do you convert big decimals to strings?

The java. math. BigDecimal. toString() method is used to represent the current BigDecimal by which this method is called into String form, using scientific notation if an exponent is needed.

Can we convert BigDecimal to String in Java?

Using valueOf() Method The method can be used to convert BigDecimal to String. It internally uses the toString() method. Therefore, using toString() method for conversion will be convenient.


2 Answers

To get exactly 10.0001 you need to use the String constructor or valueOf (which constructs a BigDecimal based on the canonical representation of the double):

BigDecimal bd = new BigDecimal("10.0001"); System.out.println(bd.toString()); // prints 10.0001 //or alternatively BigDecimal bd = BigDecimal.valueOf(10.0001); System.out.println(bd.toString()); // prints 10.0001 

The problem with new BigDecimal(10.0001) is that the argument is a double and it happens that doubles can't represent 10.0001 exactly. So 10.0001 is "transformed" to the closest possible double, which is 10.000099999999999766941982670687139034271240234375 and that's what your BigDecimal shows.

For that reason, it rarely makes sense to use the double constructor.

You can read more about it here, Moving decimal places over in a double

like image 76
assylias Avatar answered Sep 30 '22 03:09

assylias


Your BigDecimal doesn't contain the number 10.0001, because you initialized it with a double, and the double didn't quite contain the number you thought it did. (This is the whole point of BigDecimal.)

If you use the string-based constructor instead:

BigDecimal bd = new BigDecimal("10.0001"); 

...then it will actually contain the number you expect.

like image 37
T.J. Crowder Avatar answered Sep 30 '22 02:09

T.J. Crowder