Microsoft
Software
Hardware
Network
Question : How can I add leading and following zero's to a floating point number?
I'm trying to display a string in the format 000.000 which will represent a calculated number
in my program. The calculated numbers will not always have 3 digits on each side of the decimal place.
for instance here are a couple of example numbers
1.22
22.34
So what I need to do is make a string from the number but the string needs to have left and right padding
of zeros, so the numbers above would be
001.220
022.340
I was trying to copy the numberString into the defaultString by position listed below, but
apparently Strings are immutable and I can't insert a substring. I don't know if there is a way
to add the padding to the number first before I make it a string or a way to do it as a string.
Either way I'm a little stumped since I've only been using Python a few weeks.
numberString = str(1.22)
decimalPosition = numberString.find('.')
length = len(numberString)
defaultString = "000.000"
startPosition = length - 1 - decimalPosition #position in default string to start
for s in range(0,decimalPosition,1)
:
defaultString[startPositio
n] = numberString[s]
startPosition = startPostion + 1
for s in range(decimalPosition+1,le
ngth,1):
defaultString[startPositio
n] = numberString[s]
startPosition = startPostion + 1
Answer : How can I add leading and following zero's to a floating point number?
Try this:
"%07.3f" % number
For example:
"%07.3f" % 1.0 = '001.000'
Random Solutions
How do I read all properties of WMI class using KIX??
Logon script to copy shortcut to desktop
Backup SBS 2003 Exchange
Add date to column or table.
oracle lite
Date Range Parameter - Last 8 weeks
Windows 2003 SBS Shuts down automatically because of Microsoft EULA
Actionscript 3.0 optimization, packages and enter_frame
Inter process communication in linux.
swapping hard drives in windows xp desktop computer