Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get number without decimal places?

Tags:

python

decimal

tunnid = int(input("Sisestage oma töötundide arv ühes nädalas: "))
tasu = int(input("Sisestage oma tunnitasu: "))

if tunnid <= 40:

    print("Teie nädalapalk on " + str(tunnid*tasu))

else:

    print("Teie nädalapalk on " + str(tunnid*tasu*1.5))

If i multiply 60*10 as else i should get 900, but program gives me 900.0 So my quiestion is, how to remove this .0 from the answer, what do i have to change in my code?

p.s Im just a beginner so don't judge please :)

like image 681
Marek Lindvest Avatar asked Oct 23 '16 20:10

Marek Lindvest


3 Answers

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)

like image 171
baxbaxwalanuksiwe Avatar answered Oct 08 '22 14:10

baxbaxwalanuksiwe


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))"
like image 25
Moinuddin Quadri Avatar answered Oct 08 '22 14:10

Moinuddin Quadri


>>> num = 900.0  # tunnid * tasu * 1.5
>>> int(num) if num == int(num) else num
900
>>> num = 900.6  # tunnid * tasu * 1.5
>>> int(num) if num == int(num) else num
900.6
like image 30
Eduardo Cuesta Avatar answered Oct 08 '22 15:10

Eduardo Cuesta