ITeye博客

[转载]java中如何判断String中的内容是否为数字?

hotpro · 2010-08-25 17:23 · 编程语言 · Java · Blog · Markdown

可以用异常来做校验

   /**
     * 判断字符串是否是整数
     */
    public static boolean isInteger(String value) {
        try {
            Integer.parseInt(value);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 判断字符串是否是浮点数
     */
    public static boolean isDouble(String value) {
        try {
            Double.parseDouble(value);
            if (value.contains("."))
                return true;
            return false;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 判断字符串是否是数字
     */
    public static boolean isNumber(String value) {
        return isInteger(value) || isDouble(value);
    }  

原文链接: http://ap0406708.iteye.com/blog/446717

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。