Most elegant isNumeric() solution for java

前端 未结 7 879
囚心锁ツ
囚心锁ツ 2021-02-13 01:31

I\'m porting a small snippet of PHP code to java right now, and I was relying on the function is_numeric($x) to determine if $x is a number or not. Th

7条回答
  •  你的背包
    2021-02-13 02:14

    Did you try Integer.parseInt()? (I'm not sure of the method name, but the Integer class has a method that creates an Integer object from strings). Or if you need to handle non-integer numbers, similar methods are available for Double objects as well. If these fail, an exception is thrown.

    If you need to parse very large numbers (larger than int/double), and don't need the exact value, then a simple regex based method might be sufficient.

提交回复
热议问题