tags:

views:

353

answers:

7

When using toString(), Double adds commas (5143 is printed as 5,143). How to disable the commas?

+1  A: 
myDouble.toString().replaceAll(",", "");
Kai
Checkout the Bozho's post there, http://stackoverflow.com/questions/2242878/how-to-print-a-double-without-commas/2242921#2242921. His point is quite valid and make a lot of sense.
Adeel Ansari
I have to admit I don't get his point at all... It seems like he's talking about the decimal separator and not the digit group separator (which this question is about).
Kai
@kai1968: The example he provided is of integer value, and mentioned that, `Double` adds comma. So, I tend to agree. I think he needs to come back to clear the confusion here.
Adeel Ansari
A: 

Double result= 5143.0 Sysout(result.toString()) gives me 5143.0... can u put the code for which u got so

Abhiram
+2  A: 

As far as I am aware, you can not disable what the toString() method returns.

My solution would be as follows:

someDouble.toString().replaceAll(",", "");

Not the most elegant solution, but it works.

adimitri
It's a hack, not a solution, imho.
Rorick
+1 because I wrote the same :). I don't find it 'not elegant' (and even better than the locale handling in the other post), cause it has no side effects and does exactly what is intended.
Kai
@Rorick: It's not only a solution, it's the best one.
Kai
If only your program is for your personal home usage.
Rorick
If you want no commas, it's a everywhere-solution. If you want no separators at all you have to enhanced it (but similar!!). If you want to solve this by manipulating the locale, you have side effects and you can't be sure what's happening somewhere else or you need to run the program in Romania (and everywhere else) with your US locale.So if the problem is to format Doubles (and not Dates or Currencies or ....) then solve it by formatting Doubles. >If only your program is for your personal home usage.<The opposite is true.
Kai
+3  A: 

Probably, you have to change your locale settings. It is taken by default from system locale, but you can override this. Read javadoc on Locale class and this little tutorial to start. Locale can be specified through command line:

java -Duser.language=en -Duser.region=US MyApplication
Rorick
+1. Actually I was in the midst of writing the same thing. Anyways, I wrote a complementary post. :)
Adeel Ansari
+6  A: 

Your problem belongs to Locale, as pointed out correctly by Rorick. However, you should look into DecimalFormat class, in case changing Locale means mess up all the things.

Look at NumberFormat class, to deal with thousand separator. Because it seems your case is regarding thousand separator instead.

Adeel Ansari
+1  A: 

Three ways:

  1. Using the DecimalFormat

    DecimalFormat df = new DecimalFormat();
    DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
    dfs.setGroupingSeparator(Character.MAX_VALUE);
    df.setDecimalFormatSymbols(dfs);
    System.out.println(df.format(doubleVar));
    
  2. (as suggested by others) just replace the comma in the string that you get

  3. Set the locale on load of your VM
Bozho
+3  A: 

Java has excellent support for formatting numbers in text in different locales with the NumberFormat class:

With current locale:

NumberFormat.getNumberInstance().format(5000000);

will get you (with swedish locale) the string: 5 000 000

...or with a specific locale (e.g. french, which also results in 5 000 000):

NumberFormat.getNumberInstance(Locale.FRANCE).format(5000000);
crunchdog