Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format the floats in specific manner?

I have two floats numbers as a result for a python code.

30.00 
3995.0081

I want to format them in such a way they have a total equal no. of digits(example 11). For example the above two digits will produce the following result.

30.000000000
3995.0081000

If you notice, the no. of degits after the decimal point in these two are unequal yet total no. of degits are the same. I tried to use the following method

print('{0:11.9f}'.format(number))

But it produces the following result which is wrong.

30.000000000
3995.008100000

Is there any method or function which can produce the desired result ?

like image 867
Arjun Kashyap Avatar asked Dec 08 '25 20:12

Arjun Kashyap


1 Answers

I assume this is for display purposes only, so a string would allow for this. The two data examples you gave both already had decimals contained within, but I don't know if that'd always be the case. If not, there would a bit extra logic; but I think this would get things started.

def sizer(input_number):
    output = str(float(input_number)) + '0' * 11  # or some number in excess of the desired number of digits 
    output = output[0:12]  # based on the example of 11 desired digits
    print(output)

sizer(30.00)
sizer(3995.0081)
like image 143
pedwards Avatar answered Dec 10 '25 09:12

pedwards



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!