/*
 * Created on 2005-5-13
 * 利用正则表达式对数字进行判断
 * 代码来源自PIGO,有改动
 
*/

package org.waityou.utils;

import java.util.regex.*;

/**
 * 
@author Ace
 *     thanks, pigo
 *
 
*/

public class ValidationNumber {

    
private static Pattern intnumPattern;

    
private static Pattern numberPattern;

    
private static Pattern doublenumPattern;

    
public static boolean isintNumber(String aString) {
        
//      检查一个字符串是否是整数 允许一个正负符号位

        
if (aString == null || aString.length() < 1{
            
return false;
        }


        
if (intnumPattern == null{
            intnumPattern 
= Pattern.compile("^[+-]?[\\d]+$");
        }


        
return intnumPattern.matcher(aString).find();
    }


    
public static boolean isNumber(String aString) {
        
//      检查一个字符串是否全部是数字

        
if (aString == null || aString.length() < 1{
            
return false;
        }


        
if (numberPattern == null{
            numberPattern 
= Pattern.compile("^[\\d]+$");     //  "^[+-]?([\\d]+)|([\\d]+\\.[\\d]+)$"
        }


        
return numberPattern.matcher(aString).find();
    }


    
public static boolean isdoubleNumber(String aString) {
        
//检查一个字符串是否是浮点小数,浮点小数至少一个数字开头,允许一个正负符号位
        
//中间只出现一个小数点,小数点之后至少一个数字

        
if (aString == null || aString.length() < 1{
            
return false;
        }


        
if (doublenumPattern == null{
            doublenumPattern 
= Pattern.compile("^[+-]?[\\d]+\\.[\\d]+$");
        }

        
return doublenumPattern.matcher(aString).find();
    }


    
public static void main(String[] args) {
        
        
// 测试
        String intnum1 = "-23423";
        String intnum2 
= "234";
        String numstring 
= "234sdf";
        String num1 
= "234234";
        String doublenum1 
= "0.123456";
        String doublenum2 
= "-0.23";
        String doublenum3 
= ".123123";

        System.out.println(
"" + isintNumber(intnum1));
        System.out.println(
"" + isintNumber(intnum2));
        System.out.println(
"" + isintNumber(numstring));

        System.out.println(
"" + isNumber(num1));
        System.out.println(
"" + isNumber(numstring));

        System.out.println(
"" + isdoubleNumber(doublenum1));
        System.out.println(
"" + isdoubleNumber(doublenum2));
        System.out.println(
"" + isdoubleNumber(doublenum3));
        System.out.println(
"" + isdoubleNumber(numstring));
    }

}