The problem isn't in the size of the double
you're using, but the precision. A double
can only store 15-16 digits of precision, even though it can hold numbers much bigger than 1016.
If you want exact decimal representations - and particularly if you're using it for currency values - you should use BigDecimal
. Sample code:
import java.text.*;
import java.math.*;
import java.util.*;
public class Test {
public static void main(String[] args) {
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(Locale.US);
BigDecimal value = new BigDecimal("12385748375889879879894375893475984.03");
System.out.println(currencyFormatter.format(value));
}
}
Output:
$12,385,748,375,889,879,879,894,375,893,475,984.03
For the record the double
literal 12385748375889879879894375893475984.03 has an exact value of 12,385,748,375,889,879,000,357,561,111,150,592.