What I'm trying to accomplish
-12.345 -> -012.345
Where left of decimal can be -999->999 (for my specific need) and n-amount of decimal digits
I came up with two methods to do this:
def pad(num, pad):
pad = pow(10,pad)
if num > 0:
return "%s" % str(num + pad)[1:]
else:
return "-%s" % str(num - pad)[2:]
def pad(num, pad):
import math
mag = 1 + int(math.log10(abs(num)))
sz = len(str(num))
return str(num).zfill(pad+sz-mag)
but this seems rather obtuse for being python. I saw a previous similar question but didn't like the answer ...
>>> "%06.2f"%3.3
'003.30'
because it assumes you already know where your decimal point is and how many decimal digits you have.
This seems like a common situation, so is there an existing better/cleaner/single fncall, way to go about this in python 2.7?