double.ToString truncates last 2 digits while I can still see them while debugging

  • A+
Category:Languages

How to I get double.ToString to not sometimes truncate last digits.

If I show a double in Visualstudio Immediate window or Quickwatch I see 17 decimals. When I convert it to a string I see 15.
There is probably a good reason for this but my problem is how to get around it.

? 0.87912328514094507 0.87912328514094507 ? 0.87912328514094507.ToString() "0,879123285140945" 

Dotnet 4.6.2 on a Win7 fwiw.

 


There are two standard mechanisms for achieving that.

If you pass "R" as the argument to ToString then you get the round trip string. That is, the string where converting the double to a string, then the string back to a double always gives you the double you started with.

If you pass "G17" as the argument then you get the standard string format but extended to 17 decimal places.

You can certainly be forgiven for not knowing that! This mechanism is not particularly discoverable. I'm not a big fan of how formatting strings have failed to evolve in the last 40 years.

For more details, read the documentation: https://docs.microsoft.com/en-us/dotnet/api/system.double.tostring

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: