Can someone please explain why int (0.4 * 10.0)
is 4
yet int ((2.4 - 2.0) * 10.0)
is 3
?
And then also please explain how to get 4
. i.e. Do I really have to do int (System.Math.Round((2.4 - 2.0) * 10.0))
?
It's just so ugly for something that should be so simple.
A float value can be converted to an int value no larger than the input by using the math. floor() function, whereas it can also be converted to an int value which is the smallest integer greater than the input using math. ceil() function. The math module is to be imported in order to use these methods.
The "F" indicates that the literal numeric value it is appended to is a float value. This is necessary information for the compiler and if not present can lead to errors or the compiler otherwise interpreting the number incorrectly.
Converting Floats to Integers Python also has a built-in function to convert floats to integers: int() . In this case, 390.8 will be converted to 390 . When converting floats to integers with the int() function, Python cuts off the decimal and remaining numbers of a float to create an integer.
The double representation of 2.4 - 2.0 is 0.399999999999. When you convert a number to int the decimal part is truncated so 3.999999999 is truncated to 3.
Actually, in .Net there is a decimal
data type which will help you with this problem, if your numbers can be represented exactly as decimal fractions.
2.4m - 2.0m == 0.4m
2.4 - 2.0 != 0.4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With