Please enable JavaScript.
Coggle requires JavaScript to display documents.
How to Round Numbers in Python
(Article here) (Infrastructure…
How to Round Numbers in Python
(Article here)
In this article, you will learn
- Why the way you round numbers is important
- How to round a number according to various rounding strategies, How to implement each method in pure Python
- How rounding affects data, Which rounding strategy minimizes this effect
- How to round numbers in Numpy arrays and Pandas DataFrames
Infrastructure
-
-
A Menagerie of Methods
Truncation
- Multiplying the number by
10^p
(10 raised to the pth power) to shift the decimal point p places to the right
- Round towards negative bias on positive values
- Round towards positive bias on negative values
- Round towards zero bias: No round bias
- Taking the integer part of the new number with
int()
-
- Shifting the decimal place p places back to the left by dividing by
10^p
-
All three of techniques are rather crude when it comes to preserving a reasonable amount of precision for a given number, because it could remove precision and alter computations
-
Round Up
- Multiplying the number by
10^p
(10 raised to the pth power) to shift the decimal point p places to the right
-
- Taking the integer part of the new number with
math.ceil()
"ceil" describe the nearest integer that is greater than or equal to a given number
-
- Shifting the decimal place p places back to the left by dividing by
10^p
-
-
-
Rounding Half Up
- Multiplying the number by
10^p
(10 raised to the pth power) to shift the decimal point p places to the right
- Adding 0.5 to the shifted value, then round down with
math.floor()
- If the digit just after the shifted decimal point is less than 5, then adding 0.5 won't change the integer part of the shifted value , so the floor is equal to the integer part
- If the first digit after the decimal place is greater than or equal to 5, then adding 0.5 will increase the integer part of the shifted value by 1, so the floor is equal to this larger integer
- Shifting the decimal place p places back to the left by dividing by
10^p
-
Round_half_up(-1.225, 2)=-1.23!=-1.22
Floating-point representation error: Most modern computers store floating-point numbers as binary decimals with 53-bit precision. Only numbers that have finite binary decimal representations that can be expressed in 53 bits are stored as an exact value. Not every number has a finite binary decimal representation
Floating-point do not have exact precision, and therefore should not be used in situations where precision is paramount. For applications where the exact precision is necessary, you can use the Decimal class from Python's decimal module
Rounding Half Down
- Multiplying the number by
10^p
(10 raised to the pth power) to shift the decimal point p places to the right
- Subtracting 0.5 to the shifted value, then round up with
math.ceil()
- If the digit just after the shifted decimal point is greater than or equal to 5,then subtracting 0.5 won't change the integer part of the shifted value, so the ceil is equal to the integer part
- If the digit just after the shifted decimal point is less than 5, then subtracting 0.5 will decrease the integer part of the shifted value by 1, so the ceil is equal to this smaller integer
- Shifting the decimal place p places back to the left by dividing by
10^p
-
-
-
The Decimal
Class
Benefits:
- Exact decimal representation
- Preservation of significant digits
- User-alterable precision
Use:
- Use the decimal module's
Decimal
class to declare number
- Create a new
Decimal
instance by passing a string containing the desired value
Decimal('0.1')
- Create a
Decimal
instance from a floating-point number will also introduce floating-point representation error
Decimal(0.1)=0.10000000...1015625
- Rounding a
Decimal
is done with the .quantize()
method
Decimal('num').quantize(Decimal('num'))
The first Decimal('num')
declares the decimal number, and the second Decimal('num')
determines the number of decimal places to round the number
- Default rounding strategy is "rounding half to even"
- Call
decimal.getcontext()
function and set the .prec
/.rounding
... to change the property used by the decimal module
`decimal.getcontext().prec = 2
decimal.getcontext().rounding = decimal.ROUND_CEILING`
Rounding Numpy Arrays
-
Use:
-
numpy.around(data, decimals)
: round half to even
-
numpy.ceil(data, decimals)
-
numpy.floor(data, decimals)
-
numpy.trunc(data, decimals)
-
Round Down
- Multiplying the number by
10^p
(10 raised to the pth power) to shift the decimal point p places to the right
-
- Taking the integer part of the new number with
math.floor()
"floor" describe the nearest integer that is less than or equal to a given number
-
- Shifting the decimal place p places back to the left by dividing by
10^p
-
-
-
-