Goulouh Anwar
Goulouh Anwar

Reputation: 767

How to get user input decimal format

Hi I am having trouble getting a numeric user input and formatting the variable to decimal like so.

usrIn = Decimal input('How much money do you have in your account? ')
print (usrIn)

or

usrIn = input('How much money do you have in your account? ')
usrIn = Decimal('{0:0.5f}'.format(usrIn))
print (usrIn)

or

usrIn = input('How much money do you have in your account? ')
print (usrIn)
accBalNZD = Decimal('{0:0.5f}'.format(usrIn))

Also I cant simply assign a variable an amount and format it for some reason it works with other stuff.

prevAmtNZD = Decimal('{0:0.5f}'.format(0.0000000))
print (prevAmtNZD )

But this does work having trouble implementing please help.

exRandUSD = Decimal('{0:0.5f}'.format(random.random()* (0.780 - 0.720) + 0.720))

Upvotes: 0

Views: 1053

Answers (2)

Whirlpool Pack
Whirlpool Pack

Reputation: 91

You first need to import Decimal. Do this by typing:

from decimal import Decimal

Then, you can simply use:

UserIn = Decimal(input("........................."))

Decimal is just another type of variable, so you use it in the same way as int(input()) or str(input())

Hope this helps. :)

Upvotes: 1

Aaron
Aaron

Reputation: 2053

It would be helpful to list the types of issues you have seen with each approach you tried.

from decimal import Decimal
usrIn = Decimal(input('How much money do you have in your account? '))
print(usrIn)

Keep in mind that the input method (in Python 3) returns a string (try printing it out with repr). That is why your second/third code snippets fail. If you cast that string to a float it would work:

from decimal import Decimal
usrIn = input('How much money do you have in your account? ')
print(repr(usrIn))
usrIn = Decimal('{0:0.5f}'.format(float(usrIn)))
print (usrIn)

Upvotes: 0

Related Questions