Double to Byte type conversion error

纵然是瞬间 提交于 2020-01-04 06:05:19

问题


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.


回答1:


if you debug your code you will see what is going on:

double d = (b / 100.0) + 2.00; Console.WriteLine(d)// => d = 2.82
double dt = d - 2.00; Console.WriteLine(dt) // => dt = 0.81999999999999984
double db = dt * 100.0; Console.WriteLine(db) // => db = 81.999999999999986
byte dbb = (byte)db; Console.WriteLine(dbb)  //=> dbb = 81, because Byte is cut off after the ","

If you use decimal instead of double it will work out.

See: https://stackoverflow.com/questions/2741903/c-sharp-4-double-minus-double-giving-precision-problems

To make it complete:

decimal d = (b / 100.0m) + 2.00m; Console.WriteLine(d); 
decimal dt = d - 2.00m; Console.WriteLine(dt);  
decimal db = dt * (decimal)100.0; Console.WriteLine(db);  
byte dbb = (byte)db; Console.WriteLine(dbb);      

You can either cast (decimal) or use the "m" behind the value.




回答2:


Instead of byte dbb = (byte)db; use explicit rounding with byte dbb = (byte)Math.Round(db);.



来源:https://stackoverflow.com/questions/40673698/double-to-byte-type-conversion-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!