The Woo
The Woo

Reputation: 18645

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.

Upvotes: 0

Views: 2826

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336108

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))

Upvotes: 3

Anuj
Anuj

Reputation: 9622

Try the following in python shell.

>>> int(100.00)
100
>>>

Upvotes: 0

Related Questions