str to int: and the winner is ...

2 views
Skip to first unread message

Greg Saunders

unread,
Apr 8, 2010, 11:58:05 PM4/8/10
to pythoncalgary
Wow. This turned out to be a really funny afterthought. My thinking was converting strings to integers is pretty mundane, but it couldn't be further from the truth. Despite the weather we still managed to pull off a meeting and the vote was unanimous, David Glass's code submission:

from decimal import Decimal, ROUND_UP, ROUND_DOWN

text = "$ 1,234, 567.89"

f = Decimal("".join(c for c in text if c.isdigit() or c == '.')).to_integral

print 'Rounded down:', f(ROUND_DOWN)
print 'Rounded up:', f(ROUND_UP)

was the hands down winner.

The prize turned out to be not a 6 pack of Joe Schmuck beer but a 1.5 L bottle of Grolsch Premium Lager. While it's not quite 6 beers ... it is 

Decimal('4.55') standard bottles equivalent.

Attached is a photo of Jerry receiving the prize on behalf of David in his absence. Actually, no one knows where David lives (or if he's of legal beer drinking age) ... could be Holland for all we know.

--> David, contact me offline to make arrangements to pick up your prize ... it's yummy and won't survive more than a week at my house :)- so if you enjoy beer, contact me soon.

The runner up was Raul's submission:

#!/usr/bin/python
import re
figure = '$ 1,234, 567.89'
pattern = re.compile('[^\d.]+', re.VERBOSE)
number = float(pattern.sub(r'', figure))
print '%.1f' % number
print '%.2f' % number
print '%.1f' % round(number, 1)
print '%.1f' % round(number, 2)
print '%d' % number
print '%d' % round(number)

Sorry, no runner up prize other than "we wish we had another gigantic bottle of beer to give you, but we don't."

Thanks all ... this was fun ... we'll do it again next month. Stay tuned.

Ciao,
Greg
img_0334.jpg
Reply all
Reply to author
Forward
0 new messages