-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Round the coverage numbers before saving to database (#447)
- Loading branch information
1 parent
0862b20
commit 07653e1
Showing
5 changed files
with
95 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from decimal import ROUND_CEILING, ROUND_FLOOR, ROUND_HALF_EVEN, Decimal | ||
|
||
|
||
def precise_round( | ||
number: Decimal, precision: int = 2, rounding: str = "down" | ||
) -> Decimal: | ||
""" | ||
Helper function to do more precise rounding given a precision and rounding strategy. | ||
:param number: Number to round | ||
:param precision: The number of decimal places to round to | ||
:param rounding: Rounding strategy to use, which can be "down", "up" or "nearest" | ||
:return: The rounded number as a Decimal object | ||
""" | ||
quantizer = Decimal("0.1") ** precision | ||
if rounding == "up": | ||
return number.quantize(quantizer, rounding=ROUND_CEILING) | ||
if rounding == "down": | ||
return number.quantize(quantizer, rounding=ROUND_FLOOR) | ||
return number.quantize(quantizer, rounding=ROUND_HALF_EVEN) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from decimal import Decimal | ||
|
||
import pytest | ||
|
||
from helpers.number import precise_round | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"number,precision,rounding,expected_rounding", | ||
[ | ||
(Decimal("1.129"), 2, "down", Decimal("1.12")), | ||
(Decimal("1.121"), 2, "up", Decimal("1.13")), | ||
(Decimal("1.125"), 1, "nearest", Decimal("1.1")), | ||
(Decimal("1.18"), 1, "nearest", Decimal("1.2")), | ||
(Decimal("1.15"), 1, "nearest", Decimal("1.2")), | ||
(Decimal("1.25"), 1, "nearest", Decimal("1.2")), | ||
], | ||
ids=[ | ||
"number rounds down", | ||
"number rounds up", | ||
"number rounds nearest (down)", | ||
"number rounds nearest (up)", | ||
"number rounds half-even (up)", | ||
"number rounds half-even (down)", | ||
], | ||
) | ||
def test_precise_round( | ||
number: Decimal, precision: int, rounding: str, expected_rounding: Decimal | ||
): | ||
assert expected_rounding == precise_round( | ||
number, precision=precision, rounding=rounding | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters