Double To Long

picoflop

Well-known member
Joined
Feb 25, 2009
Messages
158
Programming Experience
5-10
Hi,

I'd like to split a double into mantissa and exponent, where mantissa should be stored in a long and exponent in an integer. Mantissa should get the full internal 17 digit value
Currently I'm doing this with
DblString = DblVar.ToString("G17")
Then splitting into the two parts, putting mantissa into Decimal, upsizing until there's no decimal point and then storing in Long.
Works good for most values, but sometimes I get something like:

d = 1.01e223
DblString = DblVar.ToString("G17")
-> DblString is "1.0100000000000001e223"
The number (if I hover the mouse over "d" in debug mode) itself is displayed correctly.
Is there a way to get the "right" value? Or is it simply a small glitch that has to be accepted, when working with double?
 
This sounds like a problem with Double. Double has a, as the name indicates, double-precision...there can be such 'error-ratios' based on how a floating-point number works. I'd suggest to to use Math.Round/Ceiling/Floor to get rid of such errors, f.e. round to the 10th value after the comma.

Bobby

Double Structure (System)
 
Back
Top