How to print decimal values ​​in python

print("enter start() to start the program") def start(): print("This script converts GBP into any currency based on the exchange rate...") print(" ") #enters a line exchangeRate = int(input("Enter the exchange rate (Eg: 0.80)")) print("how much would you like to convert???") gpb = int(input()) print(gpb*exchangeRate) 

If I set the exchange rate at 0.81 and I enter Β£ 1, it always returns 0.

+4
source share
2 answers

Use float() instead of int() with your input() call. I.e.

  gpb = float(input()) 

otherwise, if the user enters 0.81 , int() truncates this to 0 during the conversion.

Using float() , you will store the decimal value as input, and your calculation will give the result you expect.

+7
source

You specified the type as int ..... these are integers (0,1,2,3,4,5,6,7,8 ....), when you multiply 1 by 0.81, you get 0.81. .... the key number for integers is to the point, in this case zero. Since the previous answer briefly said just change the type of the variable.

0
source

Source: https://habr.com/ru/post/1434049/


All Articles