Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add commas (grouping separator) to number without modifying decimals?

I'm trying to format a string to add commas between 3 digit groups

EG:

1200.20 >> 1,200.20
15000   >> 15,000

I'm trying to figure out how to do it with DecimalFormat, to this point I have been using a script of my own that seems overly complicated. I cannot figure out how to do it, using # simply hides trailing zeroes and using 0 adds them to the number.

This is what I'm trying right now:

DecimalFormat df = new DecimalFormat("###,###.####", new DecimalFormatSymbols(Locale.US));
resultStr = df.format(Double.valueOf(resultStr));

I'm sure it must be easy but I'm not sure how to do it. I don't have to do it with DecimalFormat, I just thought it would be the easier way. How can I simply add the commas without modifying the decimals in any way?

like image 302
lisovaccaro Avatar asked Mar 10 '13 22:03

lisovaccaro


People also ask

How do I change the number separator in Excel?

Click File > Options. On the Advanced tab, under Editing options, clear the Use system separators check box. Type new separators in the Decimal separator and Thousands separator boxes.

How do you add commas to numbers?

What are the rules for using commas? First comma is placed three digits from the right of the number to form thousands, second comma is placed next two digits from the right of the number, to mark lakhs and third comma is placed after another next two digits from the right to mark crore, in Indian system of numeration.

What is used as a separator for decimal numbers?

The decimal separator is also called the radix character. Likewise, while the U.K. and U.S. use a comma to separate groups of thousands, many other countries use a period instead, and some countries separate thousands groups with a thin space.


2 Answers

You should use a NumberFormat object and set it to use grouping. Something like

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class NumberFormatEg {
   public static void main(String[] args) {
      NumberFormat myFormat = NumberFormat.getInstance();
      myFormat.setGroupingUsed(true);

      double[] numbers = { 11220.00, 232323232.24, 121211.55, 102.121212 };

      for (double d : numbers) {
         System.out.println(myFormat.format(d));
      }
      System.out.println();

      DecimalFormat decimalFormat = new DecimalFormat("#.00");
      decimalFormat.setGroupingUsed(true);
      decimalFormat.setGroupingSize(3);

      for (double d : numbers) {
         System.out.println(decimalFormat.format(d));
      }

      System.out.println("\nFor Germany");

      NumberFormat anotherFormat = NumberFormat
            .getNumberInstance(Locale.GERMAN);
      if (anotherFormat instanceof DecimalFormat) {
         DecimalFormat anotherDFormat = (DecimalFormat) anotherFormat;
         anotherDFormat.applyPattern("#.00");
         anotherDFormat.setGroupingUsed(true);
         anotherDFormat.setGroupingSize(3);

         for (double d : numbers) {
            System.out.println(anotherDFormat.format(d));
         }

      }

      System.out.println("\nFor US:");

      anotherFormat = NumberFormat.getNumberInstance(Locale.US);
      if (anotherFormat instanceof DecimalFormat) {
         DecimalFormat anotherDFormat = (DecimalFormat) anotherFormat;
         anotherDFormat.applyPattern("#.00");
         anotherDFormat.setGroupingUsed(true);
         anotherDFormat.setGroupingSize(3);

         for (double d : numbers) {
            System.out.println(anotherDFormat.format(d));
         }

      }
   }
}

which returns:

11,220
232,323,232.24
121,211.55
102.121

11,220.00
232,323,232.24
121,211.55
102.12

For Germany
11.220,00
232.323.232,24
121.211,55
102,12

For US:
11,220.00
232,323,232.24
121,211.55
102.12

An advantage of this is that the solution can be locale specific.

Edited
Now shows an example with a DecimalFormat object. Note that you should set the grouping size if you use this.

like image 128
Hovercraft Full Of Eels Avatar answered Oct 05 '22 08:10

Hovercraft Full Of Eels


You can also try something like

DecimalFormat df = new DecimalFormat("#,###.00");

System.out.println(df.format(1200.20));
System.out.println(df.format(15000));
System.out.println(df.format(123456789.99));
1,200.20
15,000.00
123,456,789.99
like image 22
arshajii Avatar answered Oct 05 '22 08:10

arshajii