在做导入/导出功能时,客户要求导出数字类型的值时,将excel相应单元格属性设为number型,由此需判断字符串值是否为数字,代码如下:

public static boolean isNumber(String number) {int index = number.indexOf(".");
if (index < 0) {
return StringUtils.isNumeric(number);
} else {
String num1 = number.substring(0, index);
String num2 = number.substring(index + 1); return StringUtils.isNumeric(num1) && StringUtils.isNumeric(num2);
}
}

网上查"java 判断字符串为数字"阅资料,大部分资料都在讲字符串转为整数的情况,很少资料提及关于负数和小数的情况,最终决定采用StringUtils.isNumberic这个方法差别,

在测试导出时发现有报错,用debug模块一点点追踪,发现StringUtils.isNumeric会将空字符串返回true,先这样解决一下:

public static boolean isNumber(String number) {
if(number==null || "".equals(number))
return false;
int index = number.indexOf(".");
if (index < 0) {
return StringUtils.isNumeric(number);
} else {
String num1 = number.substring(0, index);
String num2 = number.substring(index + 1); return StringUtils.isNumeric(num1) && StringUtils.isNumeric(num2);
}
}

网上继续查找,这个方法怎么会有这种情况,最终在官网上找到其他程序员提出的这个问题及java开发团队的回答(原文链接:https://issues.apache.org/jira/browse/LANG-428 ),可能在未来某个版本中会把这个问题给解决掉,在此记录下。

StringUtils.isNumeric使用的更多相关文章

  1. 肯爹的 StringUtils.isNumeric(String str)

    在项目中遇到一处bug,调试的结果竟然是StringUtils.isNumeric(String str) 在捣鬼(采用的是org.apache.commons.lang.StringUtils),下 ...

  2. StringUtils.isNumeric(String str) 的一个坑(转)

    在项目中遇到一处bug,调试的结果竟然是StringUtils.isNumeric(String str) 在捣鬼(采用的是org.apache.commons.lang.StringUtils),下 ...

  3. StringUtils.isNumeric()的特殊点

    String str = "-1"; StringUtils.isNumeric(str) 返回的是false StringUtils.isNumeric()方法在判断字符串是否是 ...

  4. StringUtils工具类

    StringUtils源码,使用的是commons-lang3-3.1包.下载地址 http://commons.apache.org/lang/download_lang.cgi 以下是String ...

  5. 12. Android框架和工具之 StringUtils(字符串操作)

    1. StringUtils介绍: StringUtils是apache commons lang库(http://commons.apache.org/proper/commons-lang/dow ...

  6. commons-lang3之StringUtils

    字符串是一种在开发中经常使用到的数据类型,对字符串的处理也变得非常重要,字符串本身有一些方法,但都没有对null做处理,而且有时可能还需要做一些额外处理才能满足我们的需求,比如,要判断某个字符串中是否 ...

  7. StringUtils常用方法+StringUtils详细介绍

    StringUtils常用方法+StringUtils详细介绍   StringUtils用法+StringUtils详细介绍博文来源:http://yijianfengvip.blog.163.co ...

  8. StringUtils详细介绍

    StringUtils详细介绍 public static void TestStr(){ #null 和 "" 操作~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...

  9. org.apache.commons.lang3.StringUtils中的StringUtils常用方法

    https://my.oschina.net/funmo/blog/615202?p=1 public static void TestStr(){ //null 和 ""操作~~ ...

随机推荐

  1. mycat1.5~1.6的一个bug

    以下语句在mysql单库中执行正常: SELECT * FROM device WHERE devicetype='AMS.Monitoring.XlCloud.QKL8154.XLCloudDevi ...

  2. ASP.ENT Core Linux 下 为 donet创建守护进程(转载)

    原文地址:http://www.cnblogs.com/savorboard/p/dotnetcore-supervisor.html 前言 在上篇文章中介绍了如何在 Docker 容器中部署我们的 ...

  3. nginx配置:支持phpfastcgi,nginx和php-cgi通信,部分nginx常量解释

    支持phpfastcgi的配置如下: server { listen       8000; server_name  localhost; root F:/home/projects/test; i ...

  4. Waring:This LinearLayout layout or its FrameLayout parent is useless; transfer the background attribute to the other view

    解决方法请参考: You have a single component (row) vertical linear layout, containing another linear layout. ...

  5. iis 下的 selfssl

    当然,如果你想省掉所有这些麻烦也行,最简单的在IIS启动SSL的方法只要3步: 1. 下载 IIS 6.0 Resource Kit Tools: http://www.microsoft.com/d ...

  6. Response.Redirect和Server.Transfer

    今天又比较闲,逛了逛园子,看看asp.net的内容,看到一篇关于这两个的比较: http://www.cnblogs.com/yunfeng8967/archive/2008/03/06/109323 ...

  7. Ztack学习笔记(1)-初识Ztack

    一.Zigbee协议 Zigbee是IEEE 802.15.4协议的代名词,是一种短距离.低功耗的无线通信技术.这一名称来源于蜜蜂的八字舞,因为蜜蜂(bee)是靠飞翔和“嗡嗡”(zig)地抖动翅膀的“ ...

  8. Sqlite: unable to open database file

    A database connect, there updated both queries (different statement, and regardless of order), after ...

  9. oracle作业

    http://blog.csdn.net/hao_ds/article/details/38382931 oracle作业各种参数的详细介绍

  10. Linux下Hadoop的简单安装

    Hadoop 的安装极为简单,一共只有三步:   安装JDK 安装Hadoop 配置Hadoop     1,安装JDK       下载JDK,ftp传到linux或者linux中下载     切换 ...