将单个转换为双倍

时间:2013-12-20 16:20:03

标签: vb.net double type-conversion floating-point-conversion

将Single值转换为Double值时出现问题。

BitStream提供的单曲是简单的2到6位十进制数,在很多情况下,简单到0.4,0.94,0.6等(我应该注意,我收到的文档说明它们是Floats(用Java) ,根据我的理解,与.NET中的Single相同。

我最终需要这些值为double,因为它们将用作Point3D Object(X,Y和Z)的坐标,并最终用于需要Double的其他应用程序的API。

但是,当我使用CDbl(valueAsSingle)或Ctype(valueAsSingle,Double)执行转换时,该数字在Double中添加了额外的小数位,在第9位和后面的小数位。这导致最终需要使用这些值的应用程序出现问题。

首先,我很好奇为什么会这样?其次,如果我只是将Single转换为String,然后执行Double.TryParse(valueAsString)

,就会出现问题

供参考,这是一个非常简单的例子。

Sub Main()
    Dim SingleX As Single = 0.4
    Dim SingleY As Single = 0.94
    Dim SingleZ As Single = 0.6

    Console.WriteLine(String.Concat("SX: ", SingleX, ControlChars.NewLine, "SY: ", SingleY, ControlChars.NewLine, "SZ: ", SingleZ, ControlChars.NewLine))

    Dim DoubleX As Double = CDbl(SingleX)
    Dim DoubleY As Double = CDbl(SingleY)
    Dim DoubleZ As Double = CDbl(SingleZ)

    Console.WriteLine(String.Concat("DX: ", DoubleX, ControlChars.NewLine, "DY: ", DoubleY, ControlChars.NewLine, "DZ: ", DoubleZ))

    Console.ReadLine()
End Sub

结果是

SX: 0.4
SY: 0.94
SZ: 0.6

DX: 0.400000005960464
DY: 0.939999997615814
DZ: 0.600000023841858

4 个答案:

答案 0 :(得分:2)

使用以下

Dim DoubleX As Double = Math.Round(Convert.ToDouble(SingleX),2)
Dim DoubleY As Double = Math.Round(Convert.ToDouble(SingleY),2)
Dim DoubleZ As Double = Math.Round(Convert.ToDouble(SingleZ),2)

2是int数字,表示你想要多少分数

所以

上面的代码将返回:

DX: 0.4
DY: 0.94
DZ: 0.6

我假设您使用的是.net 4.0

答案 1 :(得分:2)

好的,所以有了同事的指针,我发现这篇Wikipedia文章讨论了单精度的准确性问题。我必须承认,在阅读时我的眼睛会釉,但也许你会有更好的时间。

我不能谈谈您的具体情况,但ToStringing / Converting应该没有太多问题。或者你可以按照Imrans的答案来围绕它。

答案 2 :(得分:1)

保持单独的值(抵抗Math.Round()诱惑)并处理输出。经过多年的尝试,我以此结束(从C#翻译为VB.NET通过http://www.developerfusion.com/tools/convert/csharp-to-vb):

<System.Runtime.CompilerServices.Extension> _
Public Shared Function Nice(x As Double, significant_digits As Integer) As String
    'Check for special numbers and non-numbers
    If Double.IsInfinity(x) OrElse Double.IsNaN(x) Then
        Return x.ToString()
    End If
    ' extract sign so we deal with positive numbers only
    Dim sign As Integer = Math.Sign(x)
    x = Math.Abs(x)
    Dim fmt As String
    x = Math.Round(x, 15)
    If x = 0 Then
        fmt = New String("#"C, significant_digits - 1)
        Return String.Format("{0:0." & fmt & "}", x)
    End If
    ' get scientific exponent, 10^3, 10^6, ...
    Dim sci As Integer = CInt(Math.Floor(Math.Log(x, 10) / 3)) * 3
    ' biases decimal when exponent is negative
    ' example 0.123 shows as 0.123 instead of 123e-3
    If sci<0 Then 
        sci += 3
    End If
    ' scale number to exponent found
    x = x * Math.Pow(10, -sci)
    ' find number of digits to the left of the decimal
    Dim dg As Integer = CInt(Math.Floor(Math.Log(x, 10))) + 1
    ' adjust decimals to display
    Dim decimals As Integer = Math.Min(significant_digits - dg, 15)
    ' format for the decimals
    fmt = New String("#"C, decimals)
    Dim num = Math.Round(x, decimals)
    If sci = 0 Then
        'no exponent
        Return String.Format("{0}{1:0." & fmt & "}", If(sign < 0, "-", String.Empty), num)
    End If
    Return String.Format("{0}{1:0." & fmt & "}e{2}", If(sign < 0, "-", String.Empty), num, sci)
End Function

以下是一些例子:

x                       Nice(x,4)
0.9f                    0.9
0.96666666f             0.9667
96666f                  96.67e3
9666666f                9.667e6
0.939999997615814e-5f   0.0094e-3
0.939999997615814f      0.94
0.939999997615814e-5f   0.94e3

答案 3 :(得分:-1)

尝试一下: 将“转换为字符串”单值转换为双值。

Dim SingleX as single = 0.4
Dim SingleX as single = 0.94
Dim SingleX as single = 0.8
Dim DoubleX, DoubleY, DoubleZ as double
Double.TryParse(SingleX.tostring, DoubleX)
Double.TryParse(SingleY.tostring, DoubleY)
Double.TryParse(SingleZ.tostring, DoubleZ)
DoubleX = 0.4
DoubleY = 0.94
DoubleZ = 0.6