python 3.x - allow input to be 2.d.p, if not prompt error msg -
i want make sure when user inputs exchange rate , amount, can input 2.d.p, if input not 2.d.p there must error message. how can without messing code?
check if number rounded 2 dp same number:
x = float(input(prompt)) if round(x, 2) != x: # x has more 2 dp
also, money applications, consider using decimal module - works same way:
from decimal import decimal x = decimal(input(prompt)) if round(x, 2) != x: # x has more 2 dp
except avoids floating point representation issues, once start doing arithmetic numbers.
Comments
Post a Comment