How to format a Floating Number to String in Python

Hi guys. Today in this tutorial we will learn about How to format a Floating Number to String in Python. Here i am giving you some method which will be helpful for to format a floating point number to string. So lets learn about of this methods. I hope this will help you.

How to format a Floating Number to String in Python

Advertisements
  1. Format a Floating Number to String in Python

    to format a Floating Number to String in Python just Use format(). Here i am using format() method to format the floating point number to string in python. Here i am taking 4f so that i am getting the four digit after the decimal point in my output. Lets learn this by given below example. I hope this will help you. mynum = 1.258486 print ("{:.4f}".format(mynum)) Output : 1.2585

  2. How to format a Floating Number to String in Python

    to format a Floating Number to String in Python just Use for loop. Here i am using for loop to format the floating point number to string in python. Here i am using format() fuction with the for loop and using 3f so that i am getting three digit after the decimal point as you can see in my output. lets learn this: mylist = [10.2548,5.2451,6.25481] for numbers in mylist: print( {:.3f}".format(numbers)) Output : 10.255 5.245 6.255

Method 1: Use format()

Advertisements

Here i am using format() method to format the floating point number to string in python. Here i am taking 4f so that i am getting the four digit after the decimal point in my output. Lets learn this by given below example. I hope this will help you.

mynum = 1.258486
print ("{:.4f}".format(mynum))

Output :

1.2585

Method 2: Use for loop

Advertisements

Here i am using for loop to format the floating point number to string in python. Here i am using format() fuction with the for loop and using 3f so that i am getting three digit after the decimal point as you can see in my output. lets learn this:

mylist = [10.2548,5.2451,6.25481]
for numbers in mylist:
    print("{:.3f}".format(numbers))

Output :

10.255
5.245
6.255

Method 3: Use f-string

Advertisements

By using f-string you can format the floating point number to string in python. here i am using f string with the for loop and using 3f for to get the three digit after the decimal point. Lets learn this method:

mylist = [10.2548,5.2451,6.25481]
for numbers in mylist:
    print(f'{numbers:9.3f}')

Output :

   10.255
    5.245
    6.255

Method 4: Use %

Advertisements

By using % you can fix the length of the digit. Here i am using %4f so i am getting the 4 digit after the decimal point in my output. So you can use % method like this way. Lets learn about of this method by given below example. Lets learn it:

mynum = 10.2541655
print ('%.4f'%mynum)

Output :

10.2541

Method 5: Use round()

By using round() function we can fix the number of the digit after decimal point. Here you have to take the number which will limit the digit after the decimal point. So this method is useful for that. Here i am taking the number for it is 2. So you can see in my output the number containing two digit after the decimal point. Lets learn this by given below example:

mynum = 10.25415
print(round(mynum,2))

Output :

10.25

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment