Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Format Float Values in Python

  • 23 Jun, 2024
  • Com 1
Python Format Float

Floating values can have many decimal places, making them difficult to read and interpret.

Formatting enables us to control the number of decimal places displayed and maintain consistency throughout our application.

Here are four ways to format floats in Python:

  1. Using f-strings (Python 3.6 or above)
  2. Using format()
  3. Using the % operator
  4. Using round()

Method 1: Using f-strings

Python 3.6 introduced f-strings (formatted string literals), which are a more readable and concise way to format strings compared to older methods, such as the % operator.

Using the Python f-strings

# Define a floating-point value
value = 123.45678

formatted_value = f"{value:.3f}"
print("Three decimals:", formatted_value)
# Output: Three decimals: 123.457

formatted_value = f"{value:.2f}"
print("Two decimals:", formatted_value)
# Output: Two decimals: 123.46

formatted_value = f"{value:.1f}"
print("One decimal:", formatted_value)
# Output: One decimal: 123.5

formatted_value = f"{value:.0f}"
print("No decimal:", formatted_value)
# Output: No decimal: 123

Method 2: Using format()

You can use the str.format() method to format floating-point numbers to a specific number of decimal places or in a specific format.

Format Float Values in Python using format()

Formatting with two decimal places

value = 123.45678

formatted_value = "{:.2f}".format(value)

print(formatted_value)
# Output: 123.46

print(type(formatted_value))
# Output: <class 'str'>

The output returns a string. To convert the output to a float, use the float() function.

print(float(formatted_value))
# Output: 123.46

print(type(float(formatted_value)))
# Output: <class 'float'>

Formatting with one decimal place

value = 123.45678

formatted_value = "{:.1f}".format(value)

print(float(formatted_value))
# Output: 123.5

Method 3: Using the % Operator

The % operator is an older way of formatting strings. It can also be used for floats and works similarly to the format() method.

Method 2 - Using the % operator

# Define a floating-point value
value = 123.45678

# Format the value to two decimal places
formatted_value = "%.2f" % value
print(formatted_value)
# Output: 123.46

# Format the value to one decimal place
formatted_value = "%.1f" % value
print(formatted_value)
# Output: 123.5

# Format the value to no decimal places
formatted_value = "%d" % value
print(formatted_value)
# Output: 123

Method 4: Using the round()

The round() function returns the floating-point number rounded off to the given digits after the decimal point.

It’s important to note that, if you want a string representation, you must convert it.

Using the round() function

# Define a floating-point value
value = 123.45678

formatted_value = round(value, 3)
print("Three decimals:", formatted_value)
# Output: 123.457

formatted_value = round(value, 2)
print("Two decimals:", formatted_value)
# Output: 123.46

formatted_value = round(value, 1)
print("One decimal:", formatted_value)
# Output: 123.5

That’s all!

Post Views: 2,761
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Get Unique Values (Remove Duplicates) from Array in JavaScript
How to Implement DataTables in Laravel 11

1 Comment

  1. Justin

    December 30, 2021 at 8:40 am

    Thank you this was really helpful.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend