How to convert English decimal to Spanish decimal using globalization in C #

im using visual studio 2008. my desktop application works in different regional settings.

in my code, I have decimal numbers in string variables in English format, I want to convert them to decimal variables, using Convert.ToDecimal()regional settings like "Spanish (Ecuador).

when i do the following:

string myNum = "0.04";  
decimal myDec = Convert.ToDecimal(myNum); // here im getting 4 instead of 0,04

when i used

Convert.ToDecimal(myNum, new CultureInfo("en-US")); // i got 0.04, but i want 0,04

I can’t add a new one CultureInfo("en-Us")for each operator, because I have used conversion operations more than 100 times.

how can i achieve this. please, help?

+3
source share
2 answers

, , CultureInfo.InvariantCulture - decimal.Parse decimal.TryParse, Convert.ToDecimal. . IIRC .

, , . :

public static decimal ParseDecimalInvariant(string text)
{
    return decimal.Parse(text, CultureInfo.InvariantCulture);
}

, , . , , !

decimal myNum = 0.04m;

, :)

+2

, . , IMO , ..

, -, , . , , CultureInfo.InvariantCulture .

, 100 ; : .

+1

Source: https://habr.com/ru/post/1790396/


All Articles