close

How to format a Floating Number to String in Python

Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about How to format a Floating Number to String in Python. We will use the different methods  to fix the width of floating numbers. so without wasting time lets learn about of this.

How to format a Floating Number to String in Python

  1. format a Floating Number to String in Python

    to format a Floating Number to String in Python just Use format(). By using format() you can fix the width of floating numbers. Lets learn about of this by given below example: num = 1.24548745 print ("{:.3f}".format(num)) Output : 1.245

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

    to format a Floating Number to String in Python just Use round().By using round() you can fix the width of floating numbers. Lets learn about of this by given below example: num = 6.548496 print(round(num,3)) Output : 6.548

  3. python format

    To format a Floating Number to String in Python just Use round().By using round() you can fix the width of floating numbers. Lets learn about of this by given below example: num = 6.548496 print(round(num,3)) Output : 6.548

Method 1: Use format()

By using format() you can fix the width of floating numbers. Lets learn about of this by given below example:

num = 1.24548745
print ("{:.3f}".format(num))

Output :

1.245

Method 2: Use round()

By using round() you can fix the width of floating numbers. Lets learn about of this by given below example:

num = 6.548496
print(round(num,3))

Output :

6.548

Method 3: In list

You can fix the whole list by using this method.you can set the width of floating numbers in the list. so this is very easy and useful. lets learn it.

list = [1.25487,2.58495,6.548965]
for numbers in list:
    print("{:.3f}".format(numbers))

Output :

1.255
2.585
6.549

Method 4: Use % operator

By using % operator you can fix the width of floating numbers. Lets learn about of this by given below example:

num = 6.5484965
print ('%.3f'%num)

Output :

6.548

Conclusion

It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?

Also, Read

Leave a Comment