VS 2019/2022 Decimal Conversion issue

Hector Manjarrez 21 Reputation points
2022-07-13T16:56:47.18+00:00

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

Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
912 questions
{count} votes

Accepted answer
  1. David Lowndes 4,691 Reputation points
    2022-07-13T17:05:18.313+00:00

    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.


3 additional answers

Sort by: Most helpful
  1. Rijwan Ansari 741 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);  
    
    1 person found this answer helpful.
    0 comments No comments

  2. 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

    0 comments No comments

  3. satya karki 986 Reputation points MVP
    2022-07-14T14:25:16.477+00:00
    0 comments No comments