Introduction
In Python, we regularly take care of numbers which have a fractional half, referred to as floating-point numbers. However what if we wish to restrict the variety of decimal factors in these numbers? This Byte will speak concerning the idea of floating-point numbers, why we would wish to restrict their decimal factors, and the way to take action utilizing Python’s built-in capabilities.
Floating-Level Numbers
Floating-point numbers, or just “floats”, are numbers which have a decimal level. In Python, you possibly can outline a float by merely together with a decimal level within the quantity, like so:
my_float = 3.14159
print(my_float)
Output:
3.14159
Why restrict the decimal factors?
You may be questioning, “Why would I wish to restrict the decimal factors of a float?” Nicely, there are various causes. Maybe you are coping with a forex worth, and also you solely want two decimal locations. Or perhaps you are calculating a share, and you do not want a excessive degree of precision and wish to make it extra readable.
Limiting the decimal factors could make your information simpler to learn and perceive.
Easy methods to Restrict a Float’s Decimal Factors
Python gives a number of methods to restrict the decimal factors of a float. We’ll cowl a few of the most typical strategies right here:
Utilizing the spherical() Operate
The spherical()
operate is a built-in Python operate that rounds a quantity to a specified variety of decimal locations. By default, it rounds to the closest entire quantity, however you possibly can go a second argument to specify the variety of decimal locations. This is how you should use it:
my_float = 3.14159
rounded_float = spherical(my_float, 2)
print(rounded_float)
Output:
3.14
On this instance, we have rounded my_float
to 2 decimal locations.
Word: The spherical()
operate makes use of “spherical half to even” rounding, also referred to as “bankers’ rounding”. Which means that if the quantity to be rounded is strictly midway between two potential values, it is going to be rounded to the closest even quantity. That is one thing to remember when you’re coping with numbers that always finish in .5.
Utilizing the format() Operate
The format()
operate is one other method to restrict a float’s decimal factors in Python. This formatting methodology gives extra management over the way you need your numbers to be displayed.
num = 12.34567
formatted_num = "{:.2f}".format(num)
print(formatted_num)
Output:
12.35
On this instance, the :.2f
contained in the curly braces {}
is a format specification for the float quantity. The .2
half specifies the precision of the numbers after the decimal. The f
on the finish stands for “fastened level” quantity, which is used to signify decimal numbers.
Word: The format()
operate would not modify the unique float quantity. As a substitute, it returns a formatted string. So if you wish to use or manipulate this quantity, you may have to convert it again right into a float.
Utilizing the Decimal Module
One other methodology to restrict a float’s decimal factors is by utilizing the Decimal
module in Python. This module gives help for quick appropriately rounded decimal floating level arithmetic.
from decimal import Decimal
num = Decimal(12.34567)
rounded_num = spherical(num, 2)
print(rounded_num)
Output:
12.35
On this instance, we first import the Decimal
module. We then convert our float quantity to a Decimal and use the spherical()
operate to restrict the decimal factors to 2.
The Decimal
module gives extra precision and management over floating level arithmetic than the built-in Python float information kind.
Rounding vs Truncating Decimal Factors
When limiting decimal factors, it is vital to know the distinction between rounding and truncating. Rounding refers to approximating a quantity to the closest worth, whereas truncating means eradicating the surplus digits with out rounding.
For example, you probably have the quantity 12.789 and also you wish to restrict it to 2 decimal factors, rounding would offer you 12.79 whereas truncating would offer you 12.78.
This is how one can truncate a float to 2 decimal factors by utilizing solely int
and a few multiplication/division:
num = 12.789
truncated_num = int(num * 100) / 100
print(truncated_num)
Output:
12.78
To realize the truncation, we multiply the float by 100, convert it to an integer to take away the surplus decimal factors, after which divide it by 100 to get the truncated worth.
Conclusion
On this Byte, we explored alternative ways to restrict a float’s decimal factors in Python utilizing the spherical()
, format()
, and Decimal
module. We additionally mentioned the distinction between rounding and truncating decimal factors. The selection of methodology you employ is essentially relies on the precise necessities of your program. The Decimal
module is a robust software for coping with floating level numbers, particularly when precision is most vital. Nonetheless, for easy rounding or formatting, the spherical()
and format()
capabilities are sometimes sufficient.