Jordan Greenberg wrote:
> def addcommas(s): # assumes type(s)==str
> b=[]
> l=len(s)
> for i,v in enumerate(s):
> i+=1 # easier to understand w/ 1-based indexing, i think.
> b.append(v)
>
> if (l-i)%3==0 and not i==l:
> b.append(',')
> return ''.
Martin Walsh wrote:
> def addcommas(f):
> """
> This amounts to reversing everything left
> of the decimal, grouping by 3s, joining
> with commas, reversing and reassembling.
> """
> # assumes type(f) == float
> left, right = ('%0.2f' % f).split('.')
> rleft = [left[::-1][i:i+3] fo
> dollarize(1234567.8901) returns-> $1,234,567,89
>:
> amount = raw_input("Enter an amount: ")
> dollar_amount = dollarize(amount)
> print dollar_amount
the solution you're creating is *slightly* different than the original
spec in the problem (Exercise 13-3). the argument to dollarize() is
s
Christopher Spears wrote:
> I'm working on an exercise from Core Python Programming. I need to create a
> function that takes a float value and returns the value as string rounded to
> obtain a financial amount. Basically, the function does this:
>
> dollarize(1234567.8901) returns-> $1,234,56