Thursday, February 29, 2024

Limiting Float Decimal Factors in Python

Must read


Introduction

In Python, we regularly cope with numbers which have a fractional half, referred to as floating-point numbers. However what if we need to restrict the variety of decimal factors in these numbers? This Byte will speak in regards to the idea of floating-point numbers, why we would need to restrict their decimal factors, and the way to take action utilizing Python’s built-in features.

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 is perhaps questioning, “Why would I need to restrict the decimal factors of a float?” Properly, there are various causes. Maybe you are coping with a foreign money 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 need to make it extra readable.

Limiting the decimal factors could make your information simpler to learn and perceive.

Find out how to Restrict a Float’s Decimal Factors

Python gives a number of methods to restrict the decimal factors of a float. We’ll cowl among the most typical strategies right here:

Utilizing the spherical() Perform

The spherical() perform is a built-in Python perform that rounds a quantity to a specified variety of decimal locations. By default, it rounds to the closest complete quantity, however you possibly can cross 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.

Be aware: The spherical() perform makes use of “spherical half to even” rounding, often known as “bankers’ rounding”. Because of this if the quantity to be rounded is precisely midway between two attainable values, it will likely be rounded to the closest even quantity. That is one thing to bear in mind in case you’re coping with numbers that usually finish in .5.

Utilizing the format() Perform

The format() perform is one other technique 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.

Be aware: The format() perform would not modify the unique float quantity. As an alternative, it returns a formatted string. So if you wish to use or manipulate this quantity, you may must convert it again right into a float.

Utilizing the Decimal Module

One other methodology to restrict a float’s decimal factors is through the use of the Decimal module in Python. This module gives assist 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() perform 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 sort.

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.

As an example, when you’ve got the quantity 12.789 and also you need 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 through the use of solely int and a few multiplication/division:

num = 12.789
truncated_num = int(num * 100) / 100
print(truncated_num)

Output:

12.78

To attain 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 other 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 basically is dependent upon the particular necessities of your program. The Decimal module is a strong instrument for coping with floating level numbers, particularly when precision is most vital. Nevertheless, for easy rounding or formatting, the spherical() and format() features are sometimes sufficient.



Supply hyperlink

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article