How to always display a BigDecimal object in full decimal format instead of scientific notation?

I have an object BigDecimal, myNumberwith unknown length. For example: 12345678.

I always want to divide this number by 1 million, so I:

myNumber.divide(BigDecimal.valueOf(1000000))

I get 12.345678.

I want to display this as the string " 12.345678" without tearing away ANY decimal places.

So i do

myNumber.divide(BigDecimal.valueOf(1000000)).toString()

This works great with the above example. But if myNumber is something ridiculously small or large, for example:

0.00000001

After dividing 0.00000001by a million and converting to a string, it appears as scientific notation, which I don't want. I want it to always be displayed in full decimal format (in this case 0.00000000000001).

Any ideas?

+5
4

, divide(), , , , .

int s = myNumber.scale();
BigDecimal result = myNumber.divide(BigDecimal.valueOf(1000000), s+6, RoundingMode.UNNECESSARY);

toPlainString() .

+3

, BigDecimal.toPlainString() - , . , , , , 1/3.

+2

BigDecimal.toString toPlainString.

+1

You can use BigDecimal.toPlainString()to return a "string representation of this BigDecimalwithout an exponent field".

On the other hand, scientific notation is returning BigDecimal.toEngineeringString().

0
source

All Articles