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

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -