如何将分数数值转换为字符串"as it is"?
How to convert fraction numeric value to string "as it is"?
我有一个这样的浮点值:0.60000002384185791
我想将此值转换为这样的字符串:0.60000002384185791(完全相同 - 没有科学记数法或裁剪精度)
但每当我尝试将此值转换为字符串时,它都会给我 "0.600000023841858"
代码:
Dim ddd As Decimal = 0.60000002384185791 '//This value is coming from some method and it may be different like 0.6000000238418579123456789012 or 102020.6000000238418579123456789012
Dim myVal as string = " Your Value is : " & ddd.ToString() '//This truncates actual value to 0.600000023841858
有什么方法可以转换该值 "as it is"(没有科学记数法,也没有削减精度)?
它在 C# 中的某些限制下工作良好,但我需要 Visual Basic 中的解决方案。跟语言有关系吗(我希望不是)
您在评论中声明:
Actually my double holds this value.
我相信你要找的是The Round-trip ("R") Format Specifier。
The round-trip ("R") format specifier attempts to ensure that a numeric value that is converted to a string is parsed back into the same numeric value. This format is supported only for the Single, Double, and BigInteger types.
For Double and Single values, the "R" format specifier in some cases
fails to successfully round-trip the original value and also offers
relatively poor performance. Instead, we recommend that you use the
"G17" format specifier for Double values and the "G9" format specifier
to successfully round-trip Single values.
示例:
Dim ddd As Decimal = 0.60000002384185791D
Dim originalDouble As Double = ddd
Dim originalDoubleAsString As String = originalDouble.ToString("R")
Console.WriteLine(originalDoubleAsString)
Dim roundTripDouble As Double = Double.Parse(originalDoubleAsString)
Console.WriteLine("double round tripped correctly: {0}", (originalDouble = roundTripDouble))
输出:
0.60000002384185791
double round tripped correctly: True
我有一个这样的浮点值:0.60000002384185791 我想将此值转换为这样的字符串:0.60000002384185791(完全相同 - 没有科学记数法或裁剪精度)
但每当我尝试将此值转换为字符串时,它都会给我 "0.600000023841858"
代码:
Dim ddd As Decimal = 0.60000002384185791 '//This value is coming from some method and it may be different like 0.6000000238418579123456789012 or 102020.6000000238418579123456789012
Dim myVal as string = " Your Value is : " & ddd.ToString() '//This truncates actual value to 0.600000023841858
有什么方法可以转换该值 "as it is"(没有科学记数法,也没有削减精度)?
它在 C# 中的某些限制下工作良好,但我需要 Visual Basic 中的解决方案。跟语言有关系吗(我希望不是)
您在评论中声明:
Actually my double holds this value.
我相信你要找的是The Round-trip ("R") Format Specifier。
The round-trip ("R") format specifier attempts to ensure that a numeric value that is converted to a string is parsed back into the same numeric value. This format is supported only for the Single, Double, and BigInteger types.
For Double and Single values, the "R" format specifier in some cases fails to successfully round-trip the original value and also offers relatively poor performance. Instead, we recommend that you use the "G17" format specifier for Double values and the "G9" format specifier to successfully round-trip Single values.
示例:
Dim ddd As Decimal = 0.60000002384185791D
Dim originalDouble As Double = ddd
Dim originalDoubleAsString As String = originalDouble.ToString("R")
Console.WriteLine(originalDoubleAsString)
Dim roundTripDouble As Double = Double.Parse(originalDoubleAsString)
Console.WriteLine("double round tripped correctly: {0}", (originalDouble = roundTripDouble))
输出:
0.60000002384185791
double round tripped correctly: True