Formatting currency digits with commas
-
05-07-2021 - |
Question
I am using a java webapp, In Java class i use a Remote server API to get values like 48938493843894
. I export these values to a .XLSX file. I want to format this number like
$48,938,493,843,894
at the java class and export to .xlsx file.
How can i do this in Java?
Solution
For formatting currency it's better to use NumberFormat.getCurrencyInstance()
(look at http://docs.oracle.com/javase/6/docs/api/java/text/NumberFormat.html#getCurrencyInstance) and for limiting fractional part there is NumberFormat.html#setMaximumFractionDigits
method.
OTHER TIPS
Use pattern ###,###.###
to format it.
String str = "48938493843894";
String pattern="###,###.###";
DecimalFormat myFormatter = new DecimalFormat(pattern);
String output = myFormatter.format(Double.parseDouble(str));
System.out.println(output);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow