When using toString()
, Double adds commas (5143 is printed as 5,143).
How to disable the commas?
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
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.
This will remove all grouping (in your case commas).
DecimalFormat df = new DecimalFormat();
df.setGroupingUsed(false);
myDouble.toString().replaceAll(",", "");
Double result= 5143.0;
Sysout(result.toString())
gives me 5143.0...
can u put the code for which u got so
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);