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
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)
Videos
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 ?
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.
How to truncate a number without rounding up in python?
e.g. 1.9456 to 1.94*,* instead of 1.95
Python setting Decimal Place range without rounding?, top answer:
def truncate(f, n):
return math.floor(f * 10 ** n) / 10 ** n
first hit on googling 'python truncate float without rounding'
One way to round number down is to convert it to integer. If you want to have 2 decimal places, first multiply number by 100, than convert it to integer and last divide it by 100. A function that does that would look something like that:
def fun(num, dec_plc):
‘’’Truncate number selected decimal place
[num]: number you want to round
[dec_plc]: number of decimal places in result
‘’’
return int(num * 10**dec_plc) / 10**dec_plc
Sry for bad editing I am on phone :/
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 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
Like if I have 8.225 I want 8.22 not 8.23 Do I have to use it as a string first then convert to float? Or is there a simpler way
You would need to reassign x to the value of x = int(x) or you could also use str.format if you just want the output formatted:
print "Het antwoord van de berekening is: {:.0f}.".format(x)
int and round will exhibit different behaviour, if you have anything >= 5 after the decimal point then int will floor but round will round up, if you want to actually use round you might want to combine the two:
In [7]: x = round(1.5)
In [8]: x
Out[8]: 2.0
In [9]: int(x)
Out[9]: 2
Or again combine with str.format:
In [10]: print "Het antwoord van de berekening is: {:.0f}".format(round(1.5))
Het antwoord van de berekening is: 2
The round() function cannot alter the x variable in place, as numbers are immutable. Instead, the rounded result is returned, which your code ignores.
Store the result back in x:
x = round(x)
This will give you a floating point number rounded to the nearest whole number.
Alternatively, use x = int(x), which gives you an integer number, but floors that number (removes the decimal portion regardless if it is closer to the next whole number or not).