Beispiel #1
0
        public decimal ParseDecimalWithoutLoss(string s, int precision)
        {
            var inp = s
                      .Replace(NonbreakableSpaceStr, "") //nonbreakable space
                      .Replace(" ", "");

            if (SupposedlyDecimalContainsUnknownChars(s))
            {
                throw new FormatException(I18n.Translate("Illegal characters in number"));
            }

            //still some Bridge bugs
            inp = inp
                  .Replace(_currentCulture.NumberFormat.NumberGroupSeparator, "")
                  .Replace(_currentCulture.NumberFormat.NumberDecimalSeparator, ".");
            var beforeAndAfter = inp.Split('.'); //Chrome ignores xx in '<html lang=xx>' and overrides '<input type=number>' decimal character with one taken from browser's language/system locale

            if (beforeAndAfter.Length < 1)
            {
                throw new Exception("bug - got zero tokens");
            }

            if (precision < 0)
            {
                throw new Exception("bug - incorrect precision");
            }

            if (precision == 0)
            {
                if (beforeAndAfter.Length > 1)
                {
                    throw new Exception(I18n.Translate("Decimal part is not allowed"));
                }

                //beforeAndAfter.Length must be 1 since here

                return(Convert.ToDecimal(beforeAndAfter[0], _currentCulture));
            }

            //precision must be > 0 since here

            if (beforeAndAfter.Length > 2)
            {
                throw new Exception(I18n.Translate("Incorrect number format"));
            }

            if (beforeAndAfter.Length == 1)
            {
                //no decimal part
                return(Convert.ToDecimal(beforeAndAfter[0], _currentCulture));
            }

            var actualFactPrecision = beforeAndAfter[1].TrimEnd('0').Length;

            if (actualFactPrecision > precision)
            {
                throw new Exception(string.Format(
                                        I18n.Translate("Error: allowed {0} digits after decimal point"), precision));
            }

            return(Convert.ToDecimal(
                       beforeAndAfter[0] + "." + beforeAndAfter[1],
                       CultureInfo.InvariantCulture));
        }
Beispiel #2
0
 public static void CannotBePastDate(DateTime?x, ISet <string> errors)
 {
     errors.IfTrueAdd(x.HasValue && x.Value.Date < DateTime.Now.Date, I18n.Translate("Cannot be past date"));
 }
Beispiel #3
0
 public static void MustBeTomorrowOrLaterNullable(DateTime?x, ISet <string> errors)
 {
     errors.IfTrueAdd(x.HasValue && x.Value.Date < DateTimeExtensions.BuildSoonestMidnight(), I18n.Translate("Must be tomorrow or later"));
 }
Beispiel #4
0
 public static void MustBePositive <T>(T x, ISet <string> errors) where T : IComparable <T>
 {
     IsBiggerThan(default(T), I18n.Translate("Must be positive"))(x, errors);
 }
Beispiel #5
0
 public static Validate <string> LimitSize(int length, string customerErrorMsg0 = null)
 {
     return((v, errors) => errors.IfTrueAdd(v != null && v.Length > length,
                                            string.Format(customerErrorMsg0 ?? I18n.Translate("Field is too long by {0} chars"), v?.Length - length)));
 }
Beispiel #6
0
 public static void MustBeNonNegative <T>(T x, ISet <string> errors) where T : IComparable <T>
 {
     IsBiggerOrEqualTo(default(T), I18n.Translate("Must be zero or more"))(x, errors);
 }
Beispiel #7
0
 public static Validate <T?> MustBeNonNegative <T>() where T : struct, IComparable <T>
 {
     return((x, errors) => errors.IfTrueAdd(
                x.HasValue && default(T).CompareTo(x.Value) > 0,
                I18n.Translate("Must be non negative")));
 }
Beispiel #8
0
 public static void IsNotNullRef <T>(T x, ISet <string> errors) where T : class
 {
     errors.IfTrueAdd(x == null, I18n.Translate("Field cannot be empty"));
 }
Beispiel #9
0
 public static void IsNotEmptyOrWhitespaceOnly(string x, ISet <string> errors)
 {
     errors.IfTrueAdd(string.IsNullOrWhiteSpace(x), I18n.Translate("Field cannot be empty"));
 }
Beispiel #10
0
 public static void IsNotNull <T>(T?x, ISet <string> errors) where T : struct
 {
     errors.IfTrueAdd(!x.HasValue, I18n.Translate("Field cannot be empty"));
 }