VB.Net 字符串加倍

发布于 2024-09-15 01:32:03 字数 286 浏览 3 评论 0原文

为什么当我使用 Dbl() 将值为“22.882”的字符串转换为 double 时,它​​会丢失精度并转换为 2288.2?

我必须使用双精度,因为我正在使用 System.Web.UI.WebControls.Unit 的构造函数 (请参阅http://msdn.microsoft.com/en-us/library/ ctewx7ch.aspx)。

Why is it that when I convert a string with value "22.882" to double, using Dbl() it loses precision and is converted to 2288.2?

I have to use a double since I'm using the constructor of System.Web.UI.WebControls.Unit
(see http://msdn.microsoft.com/en-us/library/ctewx7ch.aspx).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葵雨 2024-09-22 01:32:03

没有明显的理由说明为什么会将值更改为 2288.2,但如果它实际上最终为 22882.0,那么您只是使用了一种不使用句点的区域性小数点分隔符。

您只需指定一种使用句点作为小数点分隔符的区域性:

 Dim d As Double = Double.Parse(theString, CultureInfo.InvariantCulture)

There is no apparent reason why it would change the value to 2288.2, but if it actually ends up as 22882.0 then you are just using a culture that doesn't use period as decimal separtor.

You just have to specify a culture that does use the period as decimal separator:

 Dim d As Double = Double.Parse(theString, CultureInfo.InvariantCulture)
执着的年纪 2024-09-22 01:32:03
Dim input As String = "22.882"
If Double.TryParse(input, Globalization.NumberStyles.Float, New Globalization.CultureInfo("en-US"), result) Then
    Return result
Else
    Return 0D ' Or error
End If
Dim input As String = "22.882"
If Double.TryParse(input, Globalization.NumberStyles.Float, New Globalization.CultureInfo("en-US"), result) Then
    Return result
Else
    Return 0D ' Or error
End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文