如何在c#中将字符串转换为双精度值

问题描述 投票:0回答:1

我正在使用 .net 框架构建简单的 c# winform 桌面应用程序。 我需要将像“172.5632”这样的字符串转换为双精度值。

我尝试了 Double.parse() 和 Convert.toString()

它们都不起作用,我收到“输入字符串格式不正确”异常。

如果我改变了“。”到 ',' (点到逗号)然后异常消失,但转换后的双精度值不正确。

我尝试了 Double.parse() 和 Convert.toString() 它们都不起作用,我收到“输入字符串格式不正确”异常。

我在win10中使用Visual Studio 2022。

使用的是dotnet4.7.2

这真的很奇怪,因为这是非常基本的问题。

c# .net string type-conversion double
1个回答
0
投票

这可能是文化问题,请确保指定不变的文化格式

string doubleNumber = "172.5632";
Console.WriteLine(double.Parse(doubleNumber, CultureInfo.InvariantCulture));
// Output: 172.5632
© www.soinside.com 2019 - 2024. All rights reserved.