Pascal said:
Hello,
I've a float number 123456789.01 and, I'de like to format it like this
"123 456 789.01".
Is this possible with % character?
The following should do what you want, although the commafy_float I
quickly added is probably a little naive:
def commafy(numstring, thousep=","):
"""
Commafy the given numeric string numstring
By default the thousands separator is a comma
"""
numlist = list(numstring)
numlist.reverse()
tmp = []
for i in range(0, len(numlist), 3):
tmp.append("".join(numlist[i:i+3]))
numlist = thousep.join(tmp)
numlist = list(numlist)
numlist.reverse()
return "".join(numlist)
def commafy_float(flStr, thousep=","):
whole, dec = flStr.split(".")
return ".".join([commafy(whole, thousep=thousep)
, dec])
if __name__ == "__main__":
units = "56746781250450"
unitsWithThouSeps = commafy(units)
print unitsWithThouSeps
aFloatAsString = "1128058.23"
aFloatAsStringWithThouSeps = commafy_float(aFloatAsString
,thousep=" ")
print aFloatAsStringWithThouSeps
Regards
-- Vincent Wehren