0

I need to convert a string to decimal.

And I'm baffled why it is not working

public ActionResult Test(){
            decimal result;
            bool o = decimal.TryParse("-2,366.15", out result);//false
            //decimal c = decimal.Parse("2,366.15", NumberStyles.AllowThousands,new CultureInfo("en-US"));

            var info = new CultureInfo("en-US");
            bool r = decimal.TryParse("2,366.15", 0, CultureInfo.CurrentCulture, out result);//false
            bool t = decimal.TryParse("2,366.15", 0, info, out result);//false
            bool e = decimal.TryParse("2,366", 0, info, out result);//false
            bool f = decimal.TryParse("-2366.15", 0, info, out result);//false
            bool h = decimal.TryParse("2366", 0, info, out result);//true
            bool h2 = decimal.TryParse("2366,15", 0, info, out result);//false
            bool h3 = decimal.TryParse("2366.15", 0, info, out result);//false

            bool g = decimal.TryParse("2,366.15", 0, CultureInfo.GetCultureInfo("en-US"), out result);//false
            bool b = decimal.TryParse("2.366,15", out result);//true
}

b and h give true;

I'm working on a European version of IIS, so b is reads without problem.

However I am trying to convert "2,366.15" which is US use of decimal and comma for numbers. Why does adding the culture info, in all it's permutations above, not work?

Tx

Vincent
  • 931
  • 7
  • 20

2 Answers2

0

I use Convert to convert strings to decimal or other number values, so to support international formats this is what I use,

  Convert.ToInt32("56789"   , CultureInfo.InvariantCulture);
  Convert.ToDouble("222,034", CultureInfo.InvariantCulture);

try

decimal.TryParse("2,3655.15", CultureInfo.InvariantCulture);
faljbour
  • 1,367
  • 2
  • 8
  • 15
0

Figured it out. it does not accept 0 as an input for numberstyles

bool f = decimal.TryParse("-2366.15", NumberStyles.Number, info, out result);//false

Vincent
  • 931
  • 7
  • 20