Format numbers in java
How do I get these formats in java?
Input:
1223893
180703
80967
1461
开发者_开发技巧700
Output :
1,223,893
180,703
80,967
1,461
700
I will be always converting one by one number, this was just to get more examples.
you can read up on java number formatting here
so you would do something like this:
DecimalFormat myFormatter = new DecimalFormat('###,###,###');
String output = myFormatter.format('1223893');
if you output the output
var it should have 1,223,893
Look for "grouping" and "thousands separator" here. DecimalFormatSymbols
provides setGroupingSeparator(',')
and you can set it on a DecimalFormat
, together with setGroupingSize(3)
. To illustrate:
DecimalFormat df = new DecimalFormat();
df.getDecimalFormatSymbols().setGroupingSeparator(',');
df.setGroupingSize(3);
System.out.println(df.format(1223893)); // prints 1,223,893
You could use DecimalFormat.
Take a look at the DecimalFormat class.
google for NumberFormat in java
See the api docs.
精彩评论