Any smart way to convert a float like this:
float f = 711989.98f;
into a decimal (or double) without losing precision?
I've tried:
decimal d = (decimal)f;
decimal d1 = (decimal)(Math.Round(f,2));
decimal d2 = Convert.ToDecimal(f);
-
More details: I'm interfacing with an old webservice that sends this huge object that has some fields as float. When I do the conversion to decimal kaboom...no more pennies !Adrian4B– Adrian4B2010年04月07日 18:32:00 +00:00Commented Apr 7, 2010 at 18:32
-
if this is coming from a web service on the wire it is probably XML, which means no float or decimals - just strings. Look where these strings are converted to "internal" formatmfeingold– mfeingold2010年04月07日 18:52:08 +00:00Commented Apr 7, 2010 at 18:52
5 Answers 5
It's too late, the 8th digit was lost in the compiler. The float type can store only 7 significant digits. You'll have to rewrite the code, assigning to double or decimal will of course solve the problem.
1 Comment
This may be a compiler bug because it seems like a valid float should convert directly to a decimal. but it wont without losing resolution. Converting 125.609375 from float to decimal will lose resolution. However, converting it from float to double and then double to decimal will keep resolution.
float float_val = 125.609375f;
decimal bad_decimal_val = (decimal)float_val; //125.6094
double double_val = (double)float_val;
decimal good_decimal_val = (decimal)double_val;
1 Comment
You lost precision the moment you've written 711989.98f.
711989.98 is decimal. With f in the end you are asking the compiler to convert it to float. This conversion cannot be done without losing precision.
What you probably want is decimal d = 711989.98m. This will not lose precision.
1 Comment
try this
float y = 20;
decimal x = Convert.ToDecimal(y/100);
print("test: " + x);
1 Comment
have you tried?
decimal.TryParse()
http://forums.asp.net/t/1161880.aspx
There are no implicit conversions between float/double and decimal. Implicit numeric conversions are always guaranteed to be without loss of precision or magnitude and will not cause an exception.
7 Comments
9007199791611905 to double will yield 9007199791611904. Converting directly to float will yield 9007200328482816. Converting to double and then to float will yield 9007199254740992 (which is off by more than if the number had been converted directly to float). Further, while compilers allow implicit conversion from float to double, such conversions are far more likely to be erroneous than would be conversions from double to float which aren't allowed but should be.float to double. However converting double to float is likely to introduce errors, depending upon the specific double value in question.double, and then wishes to draw it using graphics routines that accept float, conversion to float would lop off precision that would generally be useless even if retained; lopping off the precision would almost certainly be consistent with programmer intent. By contrast, if some code e.g. divides an int or long by a float and stores the result in a double, then unless the programmer explicitly cast the result of the division to float I would not presume that the programmer...float precision. Given int x=16777217; float y=1.0f; do you think it more likely that a programmer who writes double z=x/y; would be expecting z to be 16777216.0 or 16777217.0? For that matter, even given float one=1.0f,ten=10.0f; double d=one/ten;, do you think a programmer would more likely be wanting d to receive the value 0.1f or 0.1 ?