When using toString()
, Double adds commas (5143 is printed as 5,143).
How to disable the commas?
views:
353answers:
7Double result= 5143.0 Sysout(result.toString()) gives me 5143.0... can u put the code for which u got so
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.
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
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.
Three ways:
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));
(as suggested by others) just replace the comma in the string that you get
- Set the locale on load of your VM
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);