Is '.' the decimal point character in your regional settings?
If your string will always have a '.' decimal point, see if using one of the ToDecimal variants that allow you to provide an invariant culture works.
VS 2019/2022 Decimal Conversion issue
I'm having an strange issue converting any string value to decimal:
string strvar = "12.67";
decimal decvar = Convert.ToDecimal(strvar);
In the above example VS 2019/2022 returns 1267 instead of 12.67.
So, basically is ignoring the decimals.
I do have VS 2019/2022 installed at home and work.
At work the conversion works fine but at home does not.
Something happened and I scanned my PC for virus, malware, etc.
Also I uninstalled/reinstalling VS and I still having the same issue.
I even tried this approach with no luck:
decimal value = decimal.Parse(s, CultureInfo.InvariantCulture);
The thing is at home worked fine before and then suddenly something changed and I'm having that issue.
I would sincerely appreciate any advice!
Thanks,
Hector
-
David Lowndes 4,721 Reputation points
2022-07-13T17:05:18.313+00:00
3 additional answers
Sort by: Most helpful
-
Rijwan Ansari 751 Reputation points MVP
2022-07-14T14:23:57.8+00:00 HI
Try in this way.
string strvar = "12.67"; double d = Double.Parse(Regex.Replace(strvar,"[.,]",separator));
This is because of culture issue.
Alternatively,
string strvar = "12.67"; double input = Convert.ToDouble(strvar,CultureInfo.InvariantCulture);
-
Hector Manjarrez 21 Reputation points
2022-07-14T13:22:50.893+00:00 David and Anna,
Thank you for your replies and suggestions. I think like you mentioned the region settings changed in my home computer.
Yesterday I was able to fix the issue adding the CultureInfo.InvariantCulture in my variables/conversions.
Without it the val1 was 167 and also doing the decimal conversion the "1.67" became "1,67":string val1 = "1.67"; decimal dec = Convert.ToDecimal(val1, CultureInfo.InvariantCulture); string[] sOrb = dec.ToString(CultureInfo.InvariantCulture).Split(new char[] { '.' }); string Orb = ""; if (sOrb[1].Length == 1) { Orb = sOrb[0] + "." + "0" + sOrb[1]; } else { Orb = dec.ToString(CultureInfo.InvariantCulture); }
I will look for the region settings this evening after work.
Best Regards,
Hector -
satya karki 991 Reputation points MVP
2022-07-14T14:25:16.477+00:00 Hi,
You can follow this thread.
https://stackoverflow.com/questions/37461888/decimal-point-ignored-in-convert-todouble