Best way to parseDouble with comma as decimal separator?

前端 未结 10 741
暗喜
暗喜 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:58

    Double.parseDouble(p.replace(',','.'))
    

    ...is very quick as it searches the underlying character array on a char-by-char basis. The string replace versions compile a RegEx to evaluate.

    Basically replace(char,char) is about 10 times quicker and since you'll be doing these kind of things in low-level code it makes sense to think about this. The Hot Spot optimiser will not figure it out... Certainly doesn't on my system.

提交回复
热议问题