0

I have a float value like this : 0.60000002384185791 I want to convert this value to string like this : 0.60000002384185791 (Exactly same - No scientific notations or clipping off precision)

But whenever I try to convert this value to string, it gives me "0.600000023841858"

Code:

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

Is there any way to convert that value "as it is" (Without scientific notations and without clipping off precision)?

It is working good upto some limit in C#, but I need solution in Visual Basic. Is there anything to do with language (I hope not).

Bharat Mori
  • 363
  • 1
  • 5
  • 14
  • Add D to the number so it’s a decimal? Might be treated as floating point and truncated on compiling – Sami Kuhmonen Oct 19 '17 at 16:38
  • 1
    As @SamiKuhmonen says, the problem in your example is that your constant `0.60000002384185791` is a Double which can't hold that many digits. Set `Option Strict On` to disallow automatic conversions and make sure that the value you get from "some method" is never converted to a double. – Blackwood Oct 19 '17 at 16:41
  • @SamiKuhmonen, @Blackwood : I have done this too, but no luck. Actually my `double` holds this value. When I hover over it, it shows me correct value i.e. `0.60000002384185791` But if I change function return type to `Decimal`, it returns me `06D` only. – Bharat Mori Oct 19 '17 at 19:14
  • According to [the MSN documentation](https://learn.microsoft.com/en-us/dotnet/api/system.double?view=netframework-4.7.1) "A Double value has up to 15 decimal digits of precision, although a maximum of 17 digits is maintained internally." – Blackwood Oct 19 '17 at 20:26
  • @Blackwood that's not really true. I would expect that the longer value is exactly what the binary fraction expands to. The number of digits of precision describes the point where changing the least significant bit in the mantissa changes the decimal translation of the binary fraction, e.g. the value might expand to ...791, but incrementing the least significant bit in the mantissa might change the value to, say, 0.803. – Craig Oct 20 '17 at 15:35
  • @BharatMori what are you trying to do with the number? Why do you care about clipping? – Craig Oct 20 '17 at 15:36
  • @Craig In my database (IBM DB2), we holds some value that has precision up-to 17 decimal places. When I convert them to string (And export them in "txt" report file), it converts double up-to 15 decimal places. I am using the same "txt" report file to create "another set of similar" data and it imports wrong data as there are up-to "15" decimal places available in file. So, I need something that converts whole database value as it is. Thanks for your time and support. – Bharat Mori Oct 23 '17 at 14:20
  • @BharatMori If you just want to export in a report, I would question the need for the full precision. If you want to transfer to another data store and exactly match the database, then you need to know the exact format of the number in DB2, and in the event that it is the same binary format as .NET, I'd strongly recommend to try to transfer using the underlying representation instead of passing through a conversion to decimal. (In the event that is is NOT the same binary format, you may not actually be able to produce the identical number.) – Craig Oct 23 '17 at 19:58
  • @Craig We save fractional numbers in DB2, and we export them in "txt" report file. Humans working on this files examine the numbers, **changes the number if they feel so** and then import those numbers in DB2 again. We are working in engineering division where we need maximum precision. We cannot even bypass the "txt conversion" - to - "human manipulation" part. They are required as they are part of our process. – Bharat Mori Oct 25 '17 at 13:31

1 Answers1

1

You state in the comments:

Actually my double holds this value.

I believe what you are seeking is the 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.

Example:

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))

Output:

0.60000002384185791

double round tripped correctly: True

TnTinMn
  • 11,522
  • 3
  • 18
  • 39
  • Thank you for your time and support. I have marked your reply as answer as it is working for me up-to 17 decimal places. We still have challenge that when decimal holds precision that is bigger than 17 chars then this won't work as double cannot hold precision more than 17 chars Like: ddd = 0.6000000238418579122233344456D myVal = ddd 'This holds 0.60000002384185791 in double and not "actual" decimal And also we cannot write something like below as it is not supported: Dim engineeringValue as String = ddd.ToString("R") '"R" is not made for Decimals – Bharat Mori Oct 23 '17 at 14:14