I need to remove decimals from float to get 6 characters after the dot WITHOUT rounding For example I have 0.00655379 and I need to get 0.006553
Videos
You can call int() on the end result:
>>> int(2.0)
2
When a number as a decimal it is usually a float in Python.
If you want to remove the decimal and keep it an integer (int). You can call the int() method on it like so...
>>> int(2.0)
2
However, int rounds down so...
>>> int(2.9)
2
If you want to round to the nearest integer you can use round:
>>> round(2.9)
3.0
>>> round(2.4)
2.0
And then call int() on that:
>>> int(round(2.9))
3
>>> int(round(2.4))
2
Hello everyone,
I am still new to python and learning.
So I practiced some exercises and made an app that calculates the percentage from the number the user enters.
My question use, how can I terminate the .0 part if the user enters an Int and keep the decimal part if they enter a float?
so for example, 5% of 100 is 5 ( Int)
and 5.1% of 100 is 5.1 (float)
Cheeky solution:
numstring = str(15.555555)
num = float(numstring[:numstring.find('.')+4])
My solution involving int abuse. int rounds towards the nearest 0. I multiply it by 10**3 to affect rounding. After using int, I divide it by 10**3 to get actual results.
It's safer, as it does work with e notation.
int(15.55555 * 10**3) / 10.0**3
I suspect you are using Python 3, because you are talking about getting the float result 1.8 when you are dividing two integers 9 and 5.
So in Python 3, there is an integer division operator // you can use:
>>> 9 // 5
1
vs
>>> 9 / 5
1.8
As for Python 2, the / operator by default does the integer division (when both operands are ints), unless you use from __future__ import division to make it behave like Python 3.
Use math.floor
Updated code:
import math
Banana = 1
Apple = 2
Cookie = 5
money = input("How much money have you got? ")
if int(money) >= 1:
print("For ", money," dollars you can get ",math.floor(int(money)/int(Banana)),"bananas")
if int(money) >= 2:
print("Or ", math.floor(int(money)/int(Apple)), "apples")
if int(money) >= 5:
print("Or ", math.floor(int(money)/int(Cookie))," cookies")
else:
print("You don't have enough money for any other imported elements in the script")
You can do:
def truncate(f, n):
return math.floor(f * 10 ** n) / 10 ** n
testing:
>>> f=1.923328437452
>>> [truncate(f, n) for n in range(7)]
[1.0, 1.9, 1.92, 1.923, 1.9233, 1.92332, 1.923328]
A super simple solution is to use strings
x = float (str (w)[:-1])
y = float (str (w)[:-2])
z = float (str (w)[:-3])
Any of the floating point library solutions would require you dodge some rounding, and using floor/powers of 10 to pick out the decimals can get a little hairy by comparison to the above.
Hello,
Does anyone know how to limit or round a float to only two decimals without rounding up?
for example,
if the number is 3.149, then I want the output to be 3.14. If the number is 3.0, then the output must be 3.00
thank you
my boyfriend has moved his excel table to python but it has added .0 to his values (eg 160 becomes 160.0) is there anyway to fix this and remove decimals ?