解析带有double.TryParse奇怪行为的数字

时间:2011-06-28 11:13:18

标签: c# parsing

为什么double.TryParse()这些设置无法解析

double.TryParse("1.035,00",
NumberStyles.AllowCurrencySymbol | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite |
NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,
GlobalSettings.Instance.DefaultCulture, out price);

其中DefaultCulture是sl-SI(斯洛文尼亚语),其中点.为数字分组符号,并且为小数点。解析后price仍为0

2 个答案:

答案 0 :(得分:6)

您遗失NumberStyles.AllowThousands

double.TryParse("1.035,00", NumberStyles.AllowCurrencySymbol | 
                            NumberStyles.AllowLeadingWhite | 
                            NumberStyles.AllowTrailingWhite |
                            NumberStyles.AllowDecimalPoint | 
                            NumberStyles.AllowLeadingSign | 
                            NumberStyles.AllowThousands,
                            GlobalSettings.Instance.DefaultCulture, out price);

答案 1 :(得分:2)

这对我有用

double.TryParse("1.035,00",
NumberStyles.Any,
GlobalSettings.Instance.DefaultCulture, out price);