Saturday 28 October 2017

python division precision





I am aware of floating point being inaccurate and would like to know
the best way to get 0.08354 instead of
(0.08353999999999999) when I do the following in
Python:



d =
8.354/100

print (d)


Answer





Use the builtin
round()
function:



>>> d =
8.354/100
>>> d
0.08353999999999999
>>>
round(d, 6)
0.08354
>>>


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...