When performing conversion from byte
to double
by mathematical operations I've got expected result.
Performing the same mathematical operations to reverse the changes in value, results in correct value as long as I keep double
type.
But when at the end I converts double
result back to byte
value the conversion results is incorrect by 1.
This is the case only for some byte
values.
Exact process:
byte b = 82; Console.WriteLine(b); // initial byte value
double d = (b / 100.0) + 2.00; Console.WriteLine(d); // 82 / 100 -> 0.82 + 2.00 -> 2.82 OK
double dt = d - 2.00; Console.WriteLine(dt); // 2.82 - 2.0 -> 0.82 OK
double db = dt * 100.0; Console.WriteLine(db); // 0.82 * 100 -> 82 (double) OK
byte dbb = (byte)db; Console.WriteLine(dbb); // (byte)82 -> 81 ERROR ERROR ERROR
b = Byte.Parse(db.ToString()); Console.WriteLine(b); // 82 -> "82" and to byte OK
Why it is happening?
The double
result does not have any values after comma.