0
float myfloat = 0.7f;

byte test = (byte)(myfloat * 10);

The result of test is 6. That is really weird. Could someone explain what happen here?

https://dotnetfiddle.net/NzZSQi

Samuel
  • 803
  • 8
  • 17
  • 1
    https://dotnetfiddle.net/IXprh6 The second duplicate explains why your dotnetfiddle and mine give different results. The first duplicate explains why floating point maths _generally_ isn't always doing what you expect. – mjwills Jul 26 '19 at 04:31
  • 1
    Note also that the results are not _necessarily_ consistent across OS and / or .NET Framework / Core versions. Hence why https://dotnetfiddle.net/4IbutH gives different results even though the code is the same. – mjwills Jul 26 '19 at 04:41
  • 1
    always best to be explicit how you want to treat a float to int conversion....```byte test = (byte)(Math.Round(myfloat * 10));``` – Keith Nicholas Jul 26 '19 at 04:44
  • Also, for less surprises, use decimal.... but understand the differences between what numbers float/doubles represent and what decimals represent – Keith Nicholas Jul 26 '19 at 04:46
  • because i want to send float data to Com Port. so I multiply them by 10. – Samuel Jul 26 '19 at 04:50
  • 1
    The short answer is that you can't expect 0.7 * 10 to equal 7. – mjwills Jul 26 '19 at 05:01

0 Answers0