Reputation: 23
I've been looking for a way to convert a double, which is big to a readable String. The double:
double myValue = 1000000000000000.123456789;
Which when printed out gives me this:
1.0000000000000001E15
The result im looking for would be this:
1.000.000.000.000.000,12
I've been searching for this for days but i couldn't find a solution for this unfortunately so i thought maybe i could ask here:) thanks for reading!
Upvotes: 0
Views: 31
Reputation: 27986
If you are interested in setting your own separator characters (i.e. not using the default for your locale) then you can do the following:
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setGroupingSeparator('.');
symbols.setDecimalSeparator(',');
DecimalFormat format = new DecimalFormat("#,##0.00", symbols);
System.out.println(format.format(100000000000.123456789));
You also mentioned rounding in your question. Unfortunately that has nothing to do with the formatting at all - you are hitting the limit of precision for double
in Java. A double
is a 64 bit floating points which gives you 15-16 digits of precision. You can verify this by checking the following expression in Java: 1000000000000000.123456789 == 1000000000000000.1
.
So, in reality what is happening is that once you have assigned your value to the myValue
variable it has already been rounded. It doesn't matter how you format it from that point you won't get the precision back.
If you need greater precision than double
you should look at BigDecimal
that supports arbitrary precision.
Upvotes: 1