简体   繁体   English

使用Visual Basic无法将具有小数点后12位的双精度/十进制值转换为字符串

[英]Unable to convert a double/decimal value with 12 decimal places to a string using visual basic

I am trying to convert a double with 12 decimal places to a string, but it only returns a string with 10 decimal places. 我试图将具有小数点后12位的双精度型转换为字符串,但是它仅返回具有十进制数后的字符串。

I have tried ToString , Format, FormatNumber, CStr and System.Convert.ToString . 我已经尝试过ToString ,Format,FormatNumber,CStr和System.Convert.ToString They all return 10 decimal places. 它们都返回10个小数位。

(Also same behavior with the decimal data type) (与十进制数据类型也具有相同的行为)

Example: 例:

d = Date.Parse(sDate).ToOADate (value is 41261.001388888886)

d.ToString = 41261.0013888889

CStr(d) = 41261.0013888889

FormatNumber(d, 12) = 41,261.001388888900

In the case of Format and FormatNumber , when 12 places are specified, the value is rounded of to 10 digits. FormatFormatNumber的情况下,当指定12个位置时,该值将四舍五入到10位数字。

This cropped up when I constructed a Filter for a DataView object. 当我为DataView对象构造一个Filter时,这种情况出现了。

Is there a way around this or is this a limitation in .Net ? 有没有办法解决此问题,或者这是.Net的限制?

In looking at the MSDN page for the Decimal Type it States: 在查看MSDN页面的十进制类型时,它会指出:

You might need to use the D type character to assign a large value to a Decimal variable or constant. 您可能需要使用d型字符一个较大的值赋给一个小数变量或常量。 This requirement is because the compiler interprets a literal as Long unless a literal type character follows the literal, as the following example shows. 此要求是因为编译器将文字解释为Long,除非文字类型字符跟在文字之后,如以下示例所示。

So based on your example 所以根据你的例子

Module Module1

    Sub Main()
        Dim d As Decimal
        d = 41261.001388888886D

        Console.WriteLine(d)
        Console.WriteLine(CStr(d))
        Console.WriteLine(FormatNumber(d, 12))
        Console.WriteLine(d.ToString("#0.000000000000"))
        Console.ReadLine()
    End Sub

End Module

Displays: 显示:

41261.001388888886
41261.001388888886
4,1261.001388888886
41261.001388888886

Give this a try it seems to work correctly 试试看,它似乎可以正常工作

Dim d As Decimal = CDec(String.Format("{0:G20}", Date.Parse(CStr(Now)).ToOADate))

Double.ToString uses only 15 digit precision. Double.ToString仅使用15位精度。

You should use d.ToString("R") 您应该使用d.ToString("R")

From MSDN : MSDN

The round-trip ("R") format specifier guarantees that a numeric value that is converted to a string will be parsed back into the same numeric value. 往返(“ R”)格式说明符可确保将转换为字符串的数值重新解析为相同的数值。

... ...

When a Single or Double value is formatted using this specifier, it is first tested using the general format, with 15 digits of precision for a Double and 7 digits of precision for a Single. 当使用此说明符格式化Single或Double值时,首先使用常规格式对其进行测试,Double的精度为15位,而Single的精度为7位。 If the value is successfully parsed back to the same numeric value, it is formatted using the general format specifier. 如果该值已成功解析回相同的数值,则使用常规格式说明符对其进行格式化。 If the value is not successfully parsed back to the same numeric value, it is formatted using 17 digits of precision for a Double and 9 digits of precision for a Single. 如果该值未成功解析回相同的数值,则使用Double的17位精度和Single的9位精度对它进行格式化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM