代码之家  ›  专栏  ›  技术社区  ›  Sumit

小数点后的长度

  •  -1
  • Sumit  · 技术社区  · 6 年前

    我增加了 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 回复  |  直到 6 年前
        1
  •  0
  •   Manish Dubey    6 年前

    我假设它是C语言,并更正了语法。你可以反过来实现你的编程语言。如果我选择一个字符串格式的数字,小数点后超过3位,它应该会出错。拒绝值(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");
                    }
                }