简体   繁体   中英

decimal.TryParse returns false

The input string in textbox is, say, $10.00 . I call

decimal result;
var a = decimal.TryParse(text, NumberStyles.AllowCurrencySymbol, cultureInfo, out result);

cultureInfo is known ( en-US ). Why does decimal.tryParse returns false?

Thank you.

问题是你已经允许货币符号本身,但是你已经省略了正确解析它所需的其他属性(例如小数点)。你真正想要的是NumberStyles.Currency

decimal.TryParse("$10.00", NumberStyles.Currency, cultureInfo, out result);

Try this, you need to include NumberStyles.Number in the bitwise combination of values for the style argument:

decimal result;
var a = decimal.TryParse(text, NumberStyles.Number | NumberStyles.AllowCurrencySymbol, cultureInfo, out result);

You forgot to allow the decimal point, too:

decimal result;
var enUS = new System.Globalization.CultureInfo("en-US");
var a = decimal.TryParse("$10.00", System.Globalization.NumberStyles.AllowCurrencySymbol | System.Globalization.NumberStyles.AllowDecimalPoint , enUS, out result);

Console.WriteLine(enUS);
Console.WriteLine(a);
Console.WriteLine(result);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM