Reputation: 48813
I need to print small numbers (percents on histograms) but java.text.DecimalFormat do something strange. For:
System.out.println((new DecimalFormat("#,##0.######")).format(123456.000123));
System.out.println((new DecimalFormat("#,##0.######")).format(0.000123));
System.out.println((new DecimalFormat("#,##0.###x###x###x###")).format(0.001));
I got:
123,456.000123
0.000123
0.001xxx
Seems java.text.DecimalFormat designed for formatting currency only...
Is it possible to get grouping in fractional part with java.text.DecimalFormat?
Or suggest any other formatting library...
I expect such or similar:
GoodFormater("0.123456") === "0.123'456"
Upvotes: 5
Views: 327
Reputation: 6969
To be honest I haven't seen any fractional numbers formatted like what you are saying, may be one reason for it not to be supported in the existing formatters. I'd also like to know why you want to format the decimal points (Just to satisfy my curiosity).
But I'd do something like this,
1.) Separate the whole amount and decimal amount. (123123.789789 into 123123 and 789789)
2.) Format them both separately. (123,123 and 789,789)
3.) Concatenate them with the .
character and create the desired output (123,123.789,789).
It's some custom work, but personally I think it's better than writing something from scratch.
Upvotes: 1
Reputation: 310957
It seems perfectly clear from the Javadoc you cited that grouping only happens before the decimal point.
Upvotes: 0