This question already has answers here:
How can I print many significant figures in Python?
(6 answers)
Closed 6 years ago.
I have an issue in printing a float number.
I tried:
a = 2
c = 4
print (str(c/a).format(1.6))
Output:
2.0
Required Output:
2.000000
How can I print up to 6 decimal places?
This can be accomplished by:
print("{:.6f}".format(your value here));
Related
This question already has answers here:
How to properly round-up half float numbers?
(21 answers)
Closed 2 years ago.
For example
round(18.5) gives me 18
round(19.5) gives me 20
I want the output of round(18.5) to be 19
math.floor(x + 0.5) will always round .5 up.
This question already has answers here:
Is floating point math broken?
(31 answers)
Python floating-point math is wrong [duplicate]
(2 answers)
Closed 4 years ago.
For example:
In: -0.01 + (-0.02) + (-0.4) + (-0.05)
Out: -0.48000000000000004
Instead of -0.48
Why does it happen so?
And how to avoid such situations?
This question already has answers here:
Python modulo on floats [duplicate]
(3 answers)
Closed 5 years ago.
def height(cent):
height= {}
height["feet"]= int(cent/30.48)
height["inch"]= int(cent%30.48)/2.54
print (height)
height (182.88)
print (182.88/30.48)
print (182.88%30.48)
The output is:
{'inch': 11, 'feet': 6}
6.0
30.479999999999993
Why does 182.88%30.48 not equal zero?
Because the value of 30.48 is really 30.4799.. This is because of the way that floating point numbers are stored in python. So when you are dividing 30.479999 by 182.88, the resulting rounded integer is 5 (i.e. 182.88 // 30.48 == 5). So the remainder is 30.47999...
This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 6 years ago.
In Python 3 vs Python 2.6, I've noticed that I can divide two integers and get a float. How do you get the Python 2.6 behaviour back?
Is there a different method to get int/int = int?
Try this:
a = 1
b = 2
int_div = a // b
This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 6 years ago.
How do I convert 45.34531 to 45.3?
Are you trying to represent it with only one digit:
print("{:.1f}".format(number)) # Python3
print "%.1f" % number # Python2
or actually round off the other decimal places?
round(number,1)
or even round strictly down?
math.floor(number*10)/10
>>> "{:.1f}".format(45.34531)
'45.3'
Or use the builtin round:
>>> round(45.34531, 1)
45.299999999999997
round(number, 1)