int will always truncate towards zero:
>>> a = 123.456
>>> int(a)
123
>>> a = 0.9999
>>> int(a)
0
>>> int(-1.5)
-1
The difference between int and math.floor is that math.floor returns the number as a float, and does not truncate towards zero.
int will always truncate towards zero:
>>> a = 123.456
>>> int(a)
123
>>> a = 0.9999
>>> int(a)
0
>>> int(-1.5)
-1
The difference between int and math.floor is that math.floor returns the number as a float, and does not truncate towards zero.
Python 2.x:
import math
int( math.floor( a ) )
N.B. Due to complicated reasons involving the handling of floats, the int cast is safe.
Python 3.x:
import math
math.floor( a )
Just convert the number with int:
print('Teie nädalapalk on {}'.format(int(tunnid * tasu * 1.5)))
Alternatively, you can use the format mini-language:
print('Teie nädalapalk on {:.0f}'.format(tunnid * tasu * 1.5))
The .0f tells the number to be truncated to 0 decimals (i.e. integer representation)
Simplest way will be to type-cast the float value to int. For example:
>>> x = 100.0
>>> x = int(x)
>>> x
100
Hence, in your code you should do:
print("Teie nädalapalk on " + str(int(tunnid*tasu)))
# Note: "str(tunnid*tasu)" replaced with "str(int(tunnid*tasu))"
General way to print floats without the .0 part
How to terminate or remove the .0 point from an int.
How to remove decimals in float?
Python: Remove division decimal - Stack Overflow
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)
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
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
Use print(str(Decimal)) instead of print(Decimal) :
print [str(i) for i in norm]
['0', '0.1', '0.2', '0.3', '0.4']
To print Decimals without the 'Decimal' appearing, you could subclass Decimal and override __repr__:
>>> class MyDecimal(Decimal):
... def __repr__(self):
... return str(float(self))
...
>>> x = Decimal(4.1)
>>> x
Decimal('4.0999999999999996447286321199499070644378662109375')
>>> y = MyDecimal(5.3)
>>> y
5.3
However, when you do any operation with them, the repr goes back to it's original form:
>>> y = MyDecimal(5.3)
>>> z = MyDecimal(4.2)
>>> y + z
Decimal('9.500000000000000000000000000')
To always get MyDecimal in such cases, you would need to override all of Decimal's operations to return a MyDecimal obeject instead of Decimal.
Note that what we're seeing here is the representation of the decimal. And you will need to leave that as is when doing other operations. Or always convert to float when using it elsewhere. From the docs:
Decimal objects cannot generally be combined with floats in arithmetic operations: an attempt to add a Decimal to a float, for example, will raise a TypeError. There’s one exception to this rule: it’s possible to use Python’s comparison operators to compare a float instance x with a Decimal instance y. Without this exception, comparisons between Decimal and float instances would follow the general rules for comparing objects of different types described in the Expressions section of the reference manual, leading to confusing results.
I want to control how many digits will be printed after the decimal point when printing a float.
The function will take a, b and p as inputs, divide a/b, and I need it to show p number of decimals.
For example, if a/b = 123.456789, then:
If p = 2, then output 123.46 If p = 3, then output 123.457 If p = 10, then output 123.4567890000
for example if i input 50 i want the value to be 0.50
Try str.format with "General format" as described here.
In [1]: print('{:.9g}'.format(8.5))
8.5
In [2]: print('{:.9g}'.format(9.0))
9
With Python 3.6+ you should consider formatted string literals (PEP 498):
x = 8.5
y = 9.0
z = 8.05
for i in (x, y, z):
print(f'{i:.9g}')
8.5
9
8.05
Manually, you can define a function to compare float and int values:
def make_str(x):
float_val = float(x)
int_val = int(float_val)
if float_val == int_val:
return str(int_val)
return str(float_val)
make_str(9.0) # '9'
make_str(8.5) # '8.5'