DecimalFormat and Double.valueOf()

后端 未结 10 1257
鱼传尺愫
鱼传尺愫 2020-12-24 15:22

I\'m trying to get rid of unnecessary symbols after decimal seperator of my double value. I\'m doing it this way:

DecimalFormat format = new DecimalFormat(\"         


        
10条回答
  •  甜味超标
    2020-12-24 15:52

    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));
    

提交回复
热议问题