Skip to content

Instantly share code, notes, and snippets.

@kognate
Last active September 19, 2019 17:52
Show Gist options
  • Save kognate/bd3ac9b7ce7f37b467ad425c4c1ea244 to your computer and use it in GitHub Desktop.
Save kognate/bd3ac9b7ce7f37b467ad425c4c1ea244 to your computer and use it in GitHub Desktop.
import decimal as d
from typing import Dict
def round_costs(cost_datum: Dict[str, object]) -> Dict[str, object]:
cd_copy = {}
for key, value in cost_datum.items():
if isinstance(value, float):
quantized = d.Decimal(value).quantize(
d.Decimal("0.01"), rounding=d.ROUND_05UP
)
cd_copy[key] = float(quantized)
else:
cd_copy[key] = value
return cd_copy
def test_rounder():
cd = {
"one": 3.14242525,
"two": "bad",
"three": 1,
"four": 9.7583,
"five": 1.0,
"six": 8.7349,
}
res = round_costs(cd)
assert res["one"] == 3.14
assert res["two"] == "bad"
assert res["three"] == 1
assert res["four"] == 9.76
assert res["five"] == 1.0
assert res["six"] == 8.73
def test_with_example():
example = {
"total_cost": 500,
"coinsurance": 0,
"code": "82805",
"net_cost": 424.919999999999999,
"deductable_remaining": {
"IND": 1.7976931348623157e308,
"FAM": 1.7976931348623157e308,
},
}
res = round_costs(example)
assert res["net_cost"] == 424.92
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment