Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format BigDecimal without scientific notation with full precision

I'd like to convert a BigDecimal to String for printing purposes but print out all digits without scientific notation. For example:

BigDecimal d = BigDecimal.valueOf(12334535345456700.12345634534534578901); String out = d.toString(); // Or perform any formatting that needs to be done System.out.println(out); 

I'd like to get 12334535345456700.12345634534534578901 printed. Right now I get: 1.23345353454567E+16.

like image 476
Mensur Avatar asked Apr 05 '13 13:04

Mensur


People also ask

How do you convert BigDecimal to double without exponential?

So, to obtain a double from a BigDecimal , simply use bd. doubleValue() . There is no need to use an intermediate string representation, and it can even be detrimental to do so, because if the string representation performs some rounding, you don't get the best approximation of the value in the BigDecimal .

What is the default precision of BigDecimal?

A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale.


1 Answers

To preserve the precision for a BigDecimal you need to pass the value in as a String

BigDecimal d = new BigDecimal("12334535345456700.12345634534534578901"); System.out.println(d.toPlainString()); 
like image 65
Reimeus Avatar answered Oct 19 '22 23:10

Reimeus