小数点后的长度

时间:2017-11-14 10:10:16

标签: decimal

如果我在小数点后输入3个以上的值,则代码会出错。但是我希望将小数点后的长度从2增加到3。

我已将precisionValue.length()>2增加到precisionValue.length()>3。但错误仍然相同

       if (str != null && str.length() > 0) {
        boolean month2 = validator.validateDecimal(str.trim());
        if (!month2) {
            errors.rejectValue(MONTH2, "10005");
        }
        if(str.contains(".")){
            String decValue = str.substring(0, str.indexOf("."));
            String precisionValue = str.substring(str.indexOf(".")+1);
            if(decValue.length()>9) {
                errors.rejectValue(MONTH2, "10035");
            }
            if(precisionValue.length()>2) {
                errors.rejectValue(MONTH2, "10038");
            }
        }
        else if(str.length()>9) {
            errors.rejectValue(MONTH2, "10035");
        }
             }

1 个答案:

答案 0 :(得分:0)

我假设它在C#中并修正了语法。你可以反过来实现你的编程语言。如果我选择一个小数点后超过3位的字符串格式的数字,它应该在errors.rejectValue(month2," 10038")中;代码行,它以预期的方式工作。我无法完全理解你的问题。

            string str = "1.2345";
            if (str != null && str.Length > 0)
            {
                bool month2 = validator.validateDecimal(str.Trim());
                if (!month2)
                {
                    errors.rejectValue(month2, "10005");
                }
                if (str.Contains("."))
                {
                    String decValue = str.Substring(0, str.IndexOf("."));
                    String precisionValue = str.Substring(str.IndexOf(".") + 1);
                    if (decValue.Length > 9)
                    {
                        errors.rejectValue(month2, "10035");
                    }
                    if (precisionValue.Length > 3)
                    {
                        errors.rejectValue(month2, "10038");
                    }
                }
                else if (str.Length > 9)
                {
                    errors.rejectValue(month2, "10035");
                }
            }
相关问题