Python, version 2.7.3 on 64-bit Ubuntu 12.04
I have a floating point number between 0 and 99.99. I need to print it as a string in this format:
WW_DD
where WW are whole digits and DD are the rounded, 2 digits past the decimal place. The string needs to be padded with 0s front and back so that it always the same format.
Some examples:
0.1 --> 00_10
1.278 --> 01_28
59.0 --> 59_00
I did the following:
def getFormattedHeight(height):
#Returns the string as: XX_XX For example: 01_25
heightWhole = math.trunc( round(height, 2) )
heightDec = math.trunc( (round(height - heightWhole, 2))*100 )
return "{:0>2d}".format(heightWhole) + "_" + "{:0>2d}".format(heightDec)
which works well except for the number 0.29, which formats to 00_28.
Can someone find a solution that works for all numbers between 0 and 99.99?