mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-25 08:44:23 -04:00
Fix significant figure round for value of 0
You can’t do `log10(0)` in maths, so we need to handle it as a special case.
This commit is contained in:
@@ -868,6 +868,8 @@ def merge_jsonlike(source, destination):
|
|||||||
|
|
||||||
|
|
||||||
def round_to_significant_figures(value, number_of_significant_figures):
|
def round_to_significant_figures(value, number_of_significant_figures):
|
||||||
|
if value == 0:
|
||||||
|
return value
|
||||||
return int(round(
|
return int(round(
|
||||||
value,
|
value,
|
||||||
number_of_significant_figures - int(floor(log10(abs(value)))) - 1
|
number_of_significant_figures - int(floor(log10(abs(value)))) - 1
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ from app.utils import (
|
|||||||
is_less_than_90_days_ago,
|
is_less_than_90_days_ago,
|
||||||
merge_jsonlike,
|
merge_jsonlike,
|
||||||
printing_today_or_tomorrow,
|
printing_today_or_tomorrow,
|
||||||
|
round_to_significant_figures,
|
||||||
)
|
)
|
||||||
from tests.conftest import fake_uuid
|
from tests.conftest import fake_uuid
|
||||||
|
|
||||||
@@ -631,3 +632,18 @@ def test_get_sample_template_returns_template(template_type):
|
|||||||
def test_merge_jsonlike_merges_jsonlike_objects_correctly(source_object, destination_object, expected_result):
|
def test_merge_jsonlike_merges_jsonlike_objects_correctly(source_object, destination_object, expected_result):
|
||||||
merge_jsonlike(source_object, destination_object)
|
merge_jsonlike(source_object, destination_object)
|
||||||
assert source_object == expected_result
|
assert source_object == expected_result
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('value, significant_figures, expected_result', (
|
||||||
|
(0, 1, 0),
|
||||||
|
(0, 2, 0),
|
||||||
|
(12_345, 1, 10_000),
|
||||||
|
(12_345, 2, 12_000),
|
||||||
|
(12_345, 3, 12_300),
|
||||||
|
(12_345, 9, 12_345),
|
||||||
|
(12_345.6789, 1, 10_000),
|
||||||
|
(12_345.6789, 9, 12_345),
|
||||||
|
(-12_345, 1, -10_000),
|
||||||
|
))
|
||||||
|
def test_round_to_significant_figures(value, significant_figures, expected_result):
|
||||||
|
assert round_to_significant_figures(value, significant_figures) == expected_result
|
||||||
|
|||||||
Reference in New Issue
Block a user