[Baypiggies] Currency to Words or how to print checks...

Paul Ivanov pivanov314 at gmail.com
Wed Jan 5 07:48:36 CET 2011


Eric Walstad, on 2011-01-04 21:56,  wrote:
> I found pynum2word on SF[1] which looks promising with nice
> looking code except that it seems to have an issue with
> floating point:
> 
> In [11]: n2w.to_cardinal(1234.56)
> Out[11]: 'one thousand, two hundred and thirty-four point five five'

Yeah, that's coming from floating point representation.
In [9]: 1234.56
Out[13]: 1234.5599999999999

but you can easily amend the n2w string to fix this for you:

In [11]: def correct_cents(num,s):
   ....:     s = s[:s.index("point")]
   ....:     s += "and %d/100" % int(num*100%100)
   ....:     return s
   ....: 


In [12]: correct_cents(1234.56,'one thousand, two hundred and thirty-four point five five')
Out[12]: 'one thousand, two hundred and thirty-four and 56/100'

Here's my 56 character code-golf version of the above:

In [12]: c=lambda n,s:s[:s.find("p")]+"and %d/100"%int(n*100%100)
In [13]: c(1234.56,'one thousand, two hundred and thirty-four point five five')
Out[13]: 'one thousand, two hundred and thirty-four and 56/100'

best,
-- 
Paul Ivanov
314 address only used for lists,  off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20110104/8681a48b/attachment.pgp>


More information about the Baypiggies mailing list