Formatting numbers in java

How to get these formats in java?

Input:

1223893 180703 80967 1461 700 

Output:

 1,223,893 180,703 80,967 1,461 700 

I will always convert one at a time, this is just to get more examples.

+2
source share
5 answers

you can read java number formatting here

so that you do something like this:

 DecimalFormat myFormatter = new DecimalFormat('###,###,###'); String output = myFormatter.format('1223893'); 

if you output var, it should have 1,223,893

+3
source

Find the "grouping" and "thousands separator" here . DecimalFormatSymbols provides setGroupingSeparator(',') , and you can set it to DecimalFormat along 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 
+3
source

You can use DecimalFormat .

+1
source

google for NumberFormat in java

See api docs .

0
source

Take a look at the DecimalFormat class.

0
source

Source: https://habr.com/ru/post/1388067/


All Articles