开发者

Python Ask For Decimal Number

Can someone please tell me how to get the code to ask for a value (say the user would enter 100.00) and put that number with it's 2 decimal points value for more functions later on, ie: multipling 开发者_开发知识库it, etc.

Thanks.


Here's a start (for Python 3; for Python 2, use raw_input instead of input):

while True:
    snum = input("Please enter a decimal number:")
    try:
        num = float(snum)
        break
    except ValueError:
        print("This is not a valid decimal number!")
print("This number, rounded to two places, is: {:0.2f}".format(num))


Try the following in python shell.

>>> int(100.00)
100
>>>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜