Skip to main content
 首页 » 编程设计

c#之将 String 转换为 Double,显式使用句点作为小数点

2024年02月06日19oomusou

我的软件的功能之一是将数字字符串转换为double 数据类型。我想明确指出使用句点 (.) 作为小数点。因此,无论用户系统的语言设置如何,它都会正确读取以句点分隔的十进制。我相信解决方案是在 Convert.ToDouble() 函数中使用 IFormatProvider 参数。我不确定该怎么做。

例子: 字符串:“3.14” 双:3.14

请您参考如下方法:

解析时使用 CultureInfo.InvariantCulture。

double d = double.Parse("3.14", CultureInfo.InvariantCulture); 

参见:CultureInfo.InvariantCulture Property

The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region.