分析双精度字符串不考虑当前区域性

Parse a double string does not consider the current culture

本文关键字:区域性 不考虑 字符串 双精度      更新时间:2023-09-26

在javascript客户端,我发送一个十进制值:

"Latitude": 80.435435344

服务器端,我收到一个字符串

"80,435435344"

然后我尝试解析字符串

valueResult.AttemptedValue = "80,435435344";
NumberFormatInfo dbNumberFormat = CultureInfo.CurrentCulture.NumberFormat;
actualValue = double.Parse(valueResult.AttemptedValue, dbNumberFormat);

我的文化是"de-DE",意思是它的

NumberFormatInfo.NumberDecimalSeparator有一个","集

但是当字符串被解析为双精度时,它的表示形式是:

80.435435344

为什么我的逗号不见了?

您可以在调试器中看到这一点,因为Visual Studio的区域性与应用程序中的区域性不同,因此当您在调试器中看到点而不是逗号时,没有任何问题,因为应用程序以正确的方式处理它

此外,当您字符串化双精度时,您应该指定格式提供程序,它使用逗号而不是点

double a = 80.435435344;
string aString = a.ToString(CultureInfo.CurrentCulture.NumberFormat);
//or
string aString = a.ToString(CultureInfo.CreateSpecificCulture("de-DE").NumberFormat);
// Both solutions return 80,435435344

你会得到你的逗号。

相关文章: