Best way to parseDouble with comma as decimal separator?

前端 未结 10 623
暗喜
暗喜 2020-11-22 06:14

Following is resulting in an Exception:

String p=\"1,234\";
Double d=Double.valueOf(p); 
System.out.println(d);

Is there a bet

10条回答
  •  粉色の甜心
    2020-11-22 06:47

    Use java.text.NumberFormat:

    NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
    Number number = format.parse("1,234");
    double d = number.doubleValue();
    

    Updated:

    To support multi-language apps use:

    NumberFormat format = NumberFormat.getInstance(Locale.getDefault());
    

提交回复
热议问题