Most elegant way to detect if a String is a number?

前端 未结 11 736

Is there a better, more elegant (and/or possibly faster) way than

boolean isNumber = false;
try{
   Double.valueOf(myNumber);
   isNumber = true;
} catch (Nu         


        
11条回答
  •  时光说笑
    2020-12-30 12:13

    See java.text.NumberFormat (javadoc).

    NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
    Number myNumber = nf.parse(myString);
    int myInt = myNumber.intValue();
    double myDouble = myNumber.doubleValue();
    

提交回复
热议问题