[Tutor] turning a number into a formated string
Marilyn Davis
marilyn at deliberate.com
Wed Dec 15 21:11:30 CET 2004
Here's one way:
left, right = str(number).split('.')
output = "%04d%d" % (int(left), int(right)) + (4 - len(right)) * '0'
Maybe someone has a more elegant way.
Hope it helps,
Marilyn Davis
On Wed, 15 Dec 2004, Ertl, John wrote:
> I need to take a number and turn it into a formatted string.
> The final output needs to look like XXXXYYYY when the X is the integer
> part padded on the left and Y is the decimal part padded on the right.
> I figured I could split the number at "." and then use zfill or something
> like this (LEVEL1 = "%04d" % LEVEL1) for the XXXX part but I am not sure
> how to right pad the decimal part of the number.
> Example.
> 1 and 1.0 needs to look like 00010000 ( I figured I would have to check the
> length of the list made from the split to see if a decimal portion existed)
> 1.1 needs to look like 00011000
> 22.33 needs to look like 00223330
> 4444.22 needs to look like 44442200
> Any ideas on the right padding the decimal side using "0"
> Thanks,
> John
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
--
More information about the Tutor
mailing list