使用Decimal.TryParse转换掩码货币字符串
本文关键字:货币 字符串 掩码 转换 Decimal TryParse 使用 | 更新日期: 2023-09-27 18:14:50
我在将此字符串转换为小数时有问题。我试着遵循这里的文档,但没有运气:十进制。TryParse方法
string stringVal = "-(3434343434.00)";
NumberStyles style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands |
NumberStyles.AllowParentheses | NumberStyles.AllowLeadingSign;
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
decimal value;
bool parseSuccess = decimal.TryParse(stringVal, style, culture, out value);
parsessuccess返回false
您的字符串中有()
和-
符号。这是不正确的,这就是为什么你的字符串不能解析。
参见AllowParentheses
的文档:
AllowParentheses:表示数字字符串可以包含一对括号。括号表示要解析的字符串表示一个负数。
我认为-号和括号同时使用是无效的。-3434343434.00和(3434343434.00)一样可以,但是-(3434343434.00)无效。-(3434343434.00)实际上没有意义,它使用了两种不同的方法来表示负数,因此有点多余。