How to format to three decimal places?

0135

You can format a floating-point number to show three decimal places using similar methods as before. Here are examples for each method:

Using Formatted String Literals (f-strings)

num = 458.541315
formatted_num = f"{num:.3f}"
print(formatted_num)  # Output: 458.541

Using the format() Method

num = 458.541315
formatted_num = "{:.3f}".format(num)
print(formatted_num)  # Output: 458.541

Using the % Operator

num = 458.541315
formatted_num = "%.3f" % num
print(formatted_num)  # Output: 458.541

All these methods will display the floating-point number with exactly three decimal places.

0 Comments

no data
Be the first to share your comment!