DecimalFormat and Double.valueOf()
The problem is that your decimal format converts your value to a localized string. I'm guessing that your default decimal separator for your locale is with a ',
'. This often happens with French locales or other parts of the world.
Basically what you need to do is create your formatted date with the '.' separator so Double.valueOf
can read it. As indicated by the comments, you can use the same format to parse the value as well instead of using Double.valueOf
.
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
symbols.setDecimalSeparator('.');
DecimalFormat format = new DecimalFormat("#.#####", symbols);
value = format.parse(format.format(41251.50000000012343));
By
get rid of unnecessary symbols after decimal seperator of my double value
do you actually mean you want to round to e.g. the 5th decimal? Then just use
value = Math.round(value*1e5)/1e5;
(of course you can also Math.floor(value*1e5)/1e5
if you really want the other digits cut off)
edit
Be very careful when using this method (or any rounding of floating points). It fails for something as simple as 265.335. The intermediate result of 265.335 * 100 (precision of 2 digits) is 26533.499999999996. This means it gets rounded down to 265.33. There simply are inherent problems when converting from floating point numbers to real decimal numbers. See EJP's answer here at https://stackoverflow.com/a/12684082/144578 - How to round a number to n decimal places in Java