P
Paul
float f = 161.71000;
double d = f; // d = 161.71000671386719
I have seen the discussions of why this happens. But unfortunately
when this is converted back to a float again it becomes 161.71001
which is unacceptable to my client. The conversion must be done due
to an external library I must use.
What is the easiest way to convert this to try to retain the original
accuracy? If the conversion to the double could just contain
something like 161.7100049 or less I would be fine - surely that sort
of precision is available with a double?
I was thinking something like Convert.ToDouble(f.ToString()+'0') which
I know is slow and inelegant, but I cannot be sure the number will
contain a decimal and testing to see if it does seems a real
tangent.
Surely this is such a classic programming problem that someone will
have solved it in a more elegant way?
Any advice would be very much appreciated!
cheers,
Paul.
double d = f; // d = 161.71000671386719
I have seen the discussions of why this happens. But unfortunately
when this is converted back to a float again it becomes 161.71001
which is unacceptable to my client. The conversion must be done due
to an external library I must use.
What is the easiest way to convert this to try to retain the original
accuracy? If the conversion to the double could just contain
something like 161.7100049 or less I would be fine - surely that sort
of precision is available with a double?
I was thinking something like Convert.ToDouble(f.ToString()+'0') which
I know is slow and inelegant, but I cannot be sure the number will
contain a decimal and testing to see if it does seems a real
tangent.
Surely this is such a classic programming problem that someone will
have solved it in a more elegant way?
Any advice would be very much appreciated!
cheers,
Paul.