java最优雅的isNumeric()解决方案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3507162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Most elegant isNumeric() solution for java
提问by Doug
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. There doesn't seem to be an equivalent function in java, and I'm not satisfied with the current solutions I've found so far.
我现在正在将一小段 PHP 代码移植到 java,并且我依靠该函数is_numeric($x)
来确定是否$x
为数字。java中似乎没有等效的功能,我对目前找到的解决方案不满意。
I'm leaning toward the regular expression solution found here: http://rosettacode.org/wiki/Determine_if_a_string_is_numeric
我倾向于在这里找到的正则表达式解决方案:http: //rosettacode.org/wiki/Determine_if_a_string_is_numeric
Which method should I use and why?
我应该使用哪种方法,为什么?
采纳答案by Jacob Mattison
Note that the PHP isNumeric() function will correctly determine that hex and scientific notation are numbers, which the regex approach you link to will not.
请注意,PHP isNumeric() 函数将正确确定十六进制和科学记数法是数字,而您链接到的正则表达式方法则不会。
One option, especially if you are already using Apache Commons libraries, is to use NumberUtils.isNumber(), from Commons-Lang. It will handle the same cases that the PHP function will handle.
一种选择,尤其是当您已经在使用 Apache Commons 库时,是使用来自 Commons-Lang 的NumberUtils.isNumber()。它将处理 PHP 函数将处理的相同情况。
回答by Zoltán Ujhelyi
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.
你试过 Integer.parseInt() 吗?(我不确定方法名称,但 Integer 类有一个从字符串创建一个 Integer 对象的方法)。或者,如果您需要处理非整数,类似的方法也可用于 Double 对象。如果这些失败,则会引发异常。
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.
如果您需要解析非常大的数字(大于 int/double),并且不需要确切的值,那么一个简单的基于正则表达式的方法可能就足够了。
回答by Colin Hebert
It's usually a bad idea to have a number in a String. If you want to use this number then parse it and use it as a numeric. You shouldn't need to "check" if it's a numeric, either you want to use it as a numeric or not.
在字符串中包含数字通常是一个坏主意。如果您想使用此数字,请解析它并将其用作数字。您不需要“检查”它是否是数字,无论您是否想将其用作数字。
If you need to convert it, then you can use every parser from Integer.parseInt(String)
to BigDecimal(String)
如果您需要转换它,那么您可以使用从Integer.parseInt(String)
到的每个解析器BigDecimal(String)
If you just need to check that the content can be seen as a numeric then you can get away with regular expressions.
如果您只需要检查内容是否可以被视为数字,那么您可以使用正则表达式。
And don't use the parseInt if your string can contain a float.
如果您的字符串可以包含浮点数,请不要使用 parseInt。
回答by Fanny H.
Have you looked into using StringUtils library? There's a isNumeric() function which might be what you're looking for. (Note that "" would be evaluated to true)
您是否考虑过使用StringUtils 库?有一个 isNumeric() 函数可能正是您要找的。(请注意,“”将被评估为真)
回答by vicsz
Optionally you can use a regular expression as well.
您也可以选择使用正则表达式。
if (theString.matches("((-|\+)?[0-9]+(\.[0-9]+)?)+")))
return true;
return false;
回答by BillThor
In a strongly typed language, a generic isNumeric(String num) method is not very useful. 13214384348934918434441 is numeric, but won't fit in most types. Many of those where is does fit won't return the same value.
在强类型语言中,通用的 isNumeric(String num) 方法不是很有用。13214384348934918434441 是数字,但不适合大多数类型。许多适合的地方不会返回相同的值。
As Colin has noted, carrying numbers in Strings withing the application is not recommended. The isNumberic function should only be applicable for input data on interface methods. These should have a more precise definition than isNumeric. Others have provided various solutions. Regular expressions can be used to test a number of conditions at once, including String length.
正如 Colin 所指出的,不建议在应用程序的字符串中携带数字。isNumberic 函数应该只适用于接口方法的输入数据。这些应该有比 isNumeric 更精确的定义。其他人提供了各种解决方案。正则表达式可用于一次测试多个条件,包括字符串长度。
回答by user2044802
Just use if((x instanceof Number)
//if checking for parsable number also
|| (x instanceof String && x.matches("((-|\+)?[0-9]+(\.[0-9]+)?)+"))
){ ... }
//---All numeric types including BigDecimal extend Number
只需使用 if((x instanceof Number)
//如果也检查可解析数
|| (x instanceof String && x.matches("((-|\+)?[0-9]+(\.[0-9]+)?)+"))
){ ... }
//--- 所有数字类型,包括 BigDecimal 扩展数字