Force Python calculation in double -
this question has answer here:
- how can force division floating point? division keeps rounding down 0 13 answers
- why doesn't division work in python? [duplicate] 4 answers
- python division 10 answers
i want print double in python. doesn't work, , don't know why.
my code:
test = 3000 / (1500 * 2000) print str(test)
i 0
, not 0.001
if following:
test = 3000 / (1500 * 2000) print '%.10f' % test
i 0.000000000
, not 0.001
.
how tell python should double?
in python 2.x need convert @ least 1 of operands float, integer division results in truncated output in python 2.x:
>>> 3000 / (1500 * 2000) 0 >>> 3000.0 / (1500 * 2000) # or use float(3000) 0.001 >>> float(3000) / (1500 * 2000) 0.001
or can import division of python 3.x in python 2.x, integer division results in true division:
>>> __future__ import division >>> 3000.0 / (1500 * 2000) 0.001
note affect division in whole module.
Comments
Post a Comment