How to get two decimal places in Python
Last Updated :
16 Dec, 2024
In Python programming, while working on financial calculations, a need to round a value to two decimal places may arise. Handling this precision of float-point values is quite easy as there are many ways to do that.
Let's explore a simple example to get two decimal places in Python.
Python
pi = 3.14159265359
print(round(pi, 2))
Explanation: The Python built-in round() function is used to round off a number to a specified number of decimal places. It takes two parameters, the number to be rounded off, and an optional parameter, the number up to which the given number is to be rounded.
Now let us see different ways to get two decimal places in Python one by one.
The f-string in Python is used to format a string. It can also be used to get 2 decimal places of a float value. It directly embeds the expression inside the curly braces.
Python
pi = 3.14159265359
print(f"{pi:.2f}")
Python format() function is an inbuilt function used to format strings. Unlike f-string, it explicitly takes the expression as arguments.
Python
pi = 3.14159265359
print("{:.2f}".format(pi))
Using % Operator
The % operator can also be used to get two decimal places in Python. It is a more traditional approach which makes the use of string formatting technique.
Python
pi = 3.14159265359
print("%.2f" % pi)
Using math Module
Python math module provides various function that can be used to perform various mathematical operations on the numbers. Once such function is the floor() function which is used to round down the float value to the nearest integer. The to get exact two decimal places, the number is to be first multiplied by 100 before applying the floor() function. And the final step includes dividing the number by 100.
Python
import math
pi = 3.14159265359
print(math.floor(pi * 100) / 100)
Using decimal Module
The decimal module provides various functions that can be used to get two decimal places of a float value. The Decimal() function is used to convert the float-point value to a decimal object. Then the quantize() function is used to set the decimal to a specific format. This function takes the format of "0.00" which means two decimal points.
Python
from decimal import Decimal
pi = 3.14159265359
# converting number to decimal value
num = Decimal(pi)
# rounding off the decimal to two decimal place
result = num.quantize(Decimal("0.00"))
print(result)
Similar Reads
Python - K length decimal Places In Python, controlling the number of decimal places in your numeric values is essential for presenting results accurately and efficiently. This article explores techniques to manage K-length decimal places in Python. Given a Decimal Number, extend its digits to K length. Input: num = 76.8, K = 5 Out
3 min read
Divide Two Integers to get Float in Python Python, a versatile and widely used programming language, offers various ways to perform mathematical operations. When it comes to dividing two integers and obtaining a float result, there are multiple approaches to achieve this task. In this article, we'll explore five different methods to divide i
2 min read
How to Round Floating Value to Two Decimals in Python In this article, we will round off a float value in Python to the nearest two decimal places. Python provides us with multiple approaches to format numbers to 2 decimal places.Using round() round() function is the most efficient way to round a number to a specified number of decimal places. It direc
2 min read
How to Convert Binary Data to Float in Python? We are given binary data and we need to convert these binary data into float using Python and print the result. In this article, we will see how to convert binary data to float in Python. Example: Input: b'\x40\x49\x0f\xdb' <class 'bytes'>Output: 3.1415927410125732 <class 'float'>Explana
2 min read
Python Program to Convert Decimal to Hexadecimal In this article, we will learn how to convert a decimal value(base 10) to a hexadecimal value (base 16) in Python. Method 1: Using hex() function hex() function is one of the built-in functions in Python3, which is used to convert an integer number into its corresponding hexadecimal form. Syntax :
3 min read