Java - Decimal Format.parse to return double value with specified number of decimal places

前端 未结 4 772
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 18:50

I want to be able to convert a string to a Double given a number of decimal places in a format string. So \"###,##0.000\" should give me a Double

4条回答
  •  死守一世寂寞
    2020-12-09 19:40

    Sure you can. Try running this:

    String in = "1,234567";
    System.out.println(NumberFormat.getNumberFormat(new Locale("fr", "FR")).parse(in));
    System.out.println(NumberFormat.getNumberFormat(new Locale("en", "GB")).parse(in));
    

    Clearly they result in different output, the first reading 1.234567 and the second 1234567. Maybe there's something wrong with your pattern? Anyway the last line there would be the preferred way of getting the UK standard format.

提交回复
热议问题