Formatting numbers

Rainer Deyke root at rainerdeyke.com
Tue May 1 21:04:50 EDT 2001


"Bjorn Pettersen" <BPettersen at NAREX.com> wrote in message
news:mailman.988759742.4786.python-list at python.org...
> Not sure what you're asking here... but the straightforward approach would
> be something like:
>
>    >>> str(12345/100.0)
>    '123.45'

You got lucky.  Never, ever use floating point if you want an exact number.
How about this:

def cents_to_string(i):
  s = '%03d' % i
  return s[:-2] + '.' + s[-2:]

print cents_to_string(12345) # Outputs '123.45'


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list