Advertisement
mosmondor

decimal settings

Sep 2nd, 2013
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.83 KB | None | 0 0
  1. public static bool HasDecimalComma;
  2. public static bool HasDecimalPeriod;
  3.  
  4. public static double GetNumber(string NumberString)
  5. {
  6.     if (!HasDecimalComma && !HasDecimalPeriod)
  7.     {
  8.         string s = string.Format("{0:0.0}", 123.123);
  9.         if (s.Contains('.'))
  10.         {
  11.             HasDecimalPeriod = true;
  12.         }
  13.         else if (s.Contains(','))
  14.         {
  15.             HasDecimalComma = true;
  16.         }
  17.         else
  18.         {
  19.             throw new SystemException(string.Format("strange number format '{0}'", s));
  20.         }
  21.     }
  22.     if (HasDecimalComma)
  23.     {
  24.         return double.Parse(NumberString.Replace('.', ','));
  25.     }
  26.     if (HasDecimalPeriod)
  27.     {
  28.         return double.Parse(NumberString.Replace(',', '.'));
  29.     }
  30.     throw new ArgumentException(string.Format("can't parse '{0}'", NumberString));
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement