How to make a cash change calculaotr?

This is the place for queries that don't fit in any of the other categories.

How to make a cash change calculaotr?

Hello
how can i make a change calculator?
for exmaple i want to buy an item thet cost 5.89\$ and in canada no more 1 cents so how can round the number to 5.90?

Posts: 15
Joined: Wed May 01, 2013 1:07 pm

Re: How to make a cash change calculaotr?

Code: Select all
`print(round(5.89, 1))`
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam

metulburr

Posts: 1838
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: How to make a cash change calculaotr?

The point is i showed only example. the number will be random. and if its 3.32 i need to round this to 3.30. so how can i do it ?

Posts: 15
Joined: Wed May 01, 2013 1:07 pm

Re: How to make a cash change calculaotr?

The point is i showed only example. the number will be random. and if its 3.32 i need to round this to 3.30. so how can i do it ?

3.3 * 5 is the same as 3.30 * 5, so if you put it in an equation it will be the same. Since you rounded it, the precision is gone anyways. However, if you are just wanting to display it in money format \$X.XX, then just use str.format()

Code: Select all
`import randomfor i in range(10):    num = random.uniform(1,10)    rounded = round(num,1)    print('{} was rounded to {:.2f}'.format(num, rounded))`
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam

metulburr

Posts: 1838
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: How to make a cash change calculaotr?

Thank you Mek......here is my noobish correction of a noobish mistake

Code: Select all
`#example1print 'Enter total:'total = input()print '\$' + str((round(total,1)))`

or with a simple loop and clear statement

Code: Select all
`import osdef cls():  '''Clears any displayed data on screen    os.system(['clear', 'cls'][os.name == 'nt]) again = 1while again == 1:    print 'Enter Total:'    total = input()    print 'Rounded total \$' + str((round(total, 1)))        another = raw_input('Another Go? y/n')    if another == 'y':      cls()      pass    elif another == 'n':      comt +=1`

I'm still learning also
Last edited by Guitarman2010 on Fri May 10, 2013 1:23 pm, edited 3 times in total.
Guitarman2010

Posts: 22
Joined: Thu Mar 07, 2013 4:39 pm
Location: USA

Re: How to make a cash change calculaotr?

Guitarman2010 wrote:Noobish way

Unfortunately wrongish way. Did you try to run it?

Code: Select all
`#exampleprint str('Enter total:') #You don't need to convert a string to a string.total = input() #You should never use input in python 2.x; use raw_input and convert your result.#String concatenation in print statements is discouraged, but you have a bigger problemprint str('\$') + (round(total,1)) #You converted the str to a str but didn't convert your float; this fails.`

-Mek
New Users, Read This
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Describe your problem; not your chosen solution.
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1500
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

Re: How to make a cash change calculaotr?

New Users, Read These Hints

Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap

Posts: 578
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe