Understanding Python 3's Decimal Formatting — :.0f vs. :.1f - AskPython (2024)

We all strive to achieve perfect outputs and answers in our code. In every programmer’s mind, there is a constant thought of displaying huge outputs in readable formats for the user. To make sure that our users can read the outputs, we need to format them in order to ensure that it looks legible.

Luckily for us programmers this process has been made easy by the format function in many programming languages. In Python, while printing the output we can specify the format fields and change them as we wish. For example, if we want to print only up to two decimal places for displaying the square root of five, we can do so in the following way:

import mathans=math.sqrt(5)print("%.2f",ans)

Formatting in Python 2 vs Python 3

Even though Python 3 is the successor of Python 2 and both of them are just different versions of the same programming language, there are key differences between the two types of Python. From keywords to print statements, a whole lot has changed with Python coming a long way since its release.

When you install Python 2 in your system, some of the functionalities of Python 3 are also installed because it supports a lot more modules and libraries that cannot be used only with Python 2.

In Python 3, the print statement which is essential for displaying output, was changed and it became a function. Unlike Python 2, where print is considered a statement. Python has also undergone changes in how it takes user input. In Python2, user-declared variables were defined by the raw_input() function, whereas now, when we use Python 3, the input() function does the same.

Despite significant changes in other areas, the format function and printing methods have largely remained consistent.

The syntax of Python 3 is also easier and more flexible than python 2. Python 2 has become obsolete nowadays and the format function can now be used in addition to the print function to display your desired output.

Understanding Float Formatting in Python 3

The primary distinction between :.0f and :.1f in Python lies in the number of decimal digits displayed in the output. :.0f signifies that only the integer part of the number is desired, without any digits following the decimal point. Conversely, :.1f indicates the desire to display one digit after the decimal point in the output.

The float data type refers to those numbers that have trailing decimal places in the base 10 number system. For example, if you write 5, it will be considered as an integer, but if you write 5.0 it will be treated as a float data type. All integers can be represented as floats, but not all floats can be represented as integers.

When carrying out complex scientific calculations, we might not need the output to be 15 decimal places long. This makes it look unreadable and messy. This can be controlled using the “.nf” format method where n is the number of decimal places required for display.

For example, when trying to divide a number whose quotient might have a lot of places after the decimal point, we can use this format specification to reduce the number of trailing digits after the point as given below:

first_num = 100second_num = 3ans = first_num / second_numprint(f"The answer before formatting is = {ans}")print(f"The answer after formatting is = {ans:.3f}")

The output would be:

The answer before formatting is = 33.333333333333336The answer after formatting is = 33.333
Understanding Python 3's Decimal Formatting — :.0f vs. :.1f - AskPython (1)

Related: Understanding the Print Format %.3f in Python.

Difference Between :.0f and :.1f in Python

The main difference between :.0f and:.1f is the number of decimal digits that should appear on the output screen after the decimal point. If we specify :.0f, it means that we only want the integer part of the number and none of the digits after the decimal point. It can also be used as a ceiling function. If the immediate digit after the decimal point is greater than or equal to 5, the integer part of the answer will increase by 1(rounding up), else the integer part will be the same and displayed.

But if we specify :.1f, it means that we only want to see one digit after the decimal point in our output.

Let’s understand the difference with the help of an example. In the given example below, we will take two user inputs, in1, and in2, and both of them should be floats. Now we’ll multiply both of those numbers to find their product. Since the product of two floats should be a float as well, we’ll be able to format the number of decimal digits after the point. First, we’ll format it to :.0f and then to :.1f .

i#first inputin1=float(input("Enter first number= "))#second inputin2=float(input("Enter second number= "))#computing the productans=in1 * in2#first displaying without formattingprint("The answer before formatting is= ",ans)#displaying after formatting upto no decimal point digits(ceiling)print("The answer after formatting without digits after decimal point is={:.0f}".format(ans))#formatting upto only the first decimal digit.print("The answer after formatting upto one decimal point is={:.1f}".format(ans))

You’ll be able to understand the difference between the two commands when you see the output.

Enter first number= 5.789Enter second number= 15.444The answer before formatting is= 89.405316The answer after formatting without digits after decimal point is=89The answer after formatting upto one decimal point is=89.4
Understanding Python 3's Decimal Formatting — :.0f vs. :.1f - AskPython (2)

Similar: What Does %s Mean in a Python Format String?

Conclusion.

This tutorial aims at providing a concrete explanation of the different types of formatting options for float data types in Python. We have looked at how the syntax of Python2 differs from that of Python3 and how it is easier to take input and format outputs in Python3 as compared to Python2. Here’s some food for thought: Do you think programmers needs string formatting more than float formatting or the opposite?

Understanding Python 3's Decimal Formatting — :.0f vs. :.1f - AskPython (2024)

References

Top Articles
Latest Posts
Article information

Author: Margart Wisoky

Last Updated:

Views: 6338

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Margart Wisoky

Birthday: 1993-05-13

Address: 2113 Abernathy Knoll, New Tamerafurt, CT 66893-2169

Phone: +25815234346805

Job: Central Developer

Hobby: Machining, Pottery, Rafting, Cosplaying, Jogging, Taekwondo, Scouting

Introduction: My name is Margart Wisoky, I am a gorgeous, shiny, successful, beautiful, adventurous, excited, pleasant person who loves writing and wants to share my knowledge and understanding with you.