I text box i have a value:
1212,12
Additionally my text box have a mask:
MaskType=Numeric
EditMask='n2'
i try to parse it:
var culture = CultureInfo.CreateSpecificCulture("en-US");
var value = decimal.Parse(myTextBox.Text, culture);
but get value=121212 when expected value=1212.12
What can be wrong?
en-US
seperates the position after decimal point with a .
instead of a ,
try another culture or set myTextBox.Text to 1212.12
You have a missmatching culture:
1212,12
and
en-US
Either use .
as a decimal separator (ie 1212.12
) or a culture which uses ,
(eg de-DE
, de-AT
, fr-FR
), but en-US
uses .
as a decimal separator and ,
as a thousands separator.
en-US
culture have NumberDecimalSeparator
as a .
not ,
but it has NumberGroupSeparator
as a ,
That's why it thinks your ,
is NumberGroupSeparator
and that's why it parses as 121212
.
As a solution, you can use different IFormatProvider
which has ,
as a NumberDecimalSeparator
or you can clone your en-US
culture with CultureInfo.Clone
method and set it's NumberDecimalSeparator
property to ,
and changing it NumberGroupSeparator
property something else.
string s = "1212,12";
var culture = (CultureInfo)CultureInfo.CreateSpecificCulture("en-US").Clone();
culture.NumberFormat.NumberDecimalSeparator = ",";
culture.NumberFormat.NumberGroupSeparator = ".";
decimal value = decimal.Parse(s, culture); // 1212.12
Either change the separator or change the culture:
For en-US culture separator is .
If you want to use separator ,
use fr-FR
culture !
var culture = CultureInfo.CreateSpecificCulture("fr-FR");
var value = decimal.Parse(myTextBox.Text, culture);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.