日期:2014-05-20  浏览次数:20620 次

判断数字为大于0小于等于1000000,整数小数都可但小数最多两位
判断数字为大于0小于等于1000000,整数小数都可但小数最多两位

------解决方案--------------------
Java code

public static void main(String[] args) {

        String str = "1";
        System.out.println(checkyonghuxiaoying(str));
        str = "1000000";
        System.out.println(checkyonghuxiaoying(str));
        str = "999999.99";
        System.out.println(checkyonghuxiaoying(str));
        str = "0";
        System.out.println(checkyonghuxiaoying(str));
        str = "999999.999";
        System.out.println(checkyonghuxiaoying(str));
        str = "1000001";
        System.out.println(checkyonghuxiaoying(str));
    }

    
    public static boolean checkyonghuxiaoying(String value) {
        boolean ret = true;
        try {
            double d = Double.parseDouble(value);
            if (d <= 0 || d > 1000000) {
                ret = false;    
            }            
        } catch (Exception e) {
            ret = false;
        }
        if (ret) {
            int index = value.lastIndexOf(".");
            if (index != -1) {
                if (value.substring(index + 1).length() > 2) {
                    ret = false;
                }
            }
        }
        return ret;
    }