diff --git a/src/corva_unit_converter/converter.py b/src/corva_unit_converter/converter.py index 9ac967a..2ed1337 100644 --- a/src/corva_unit_converter/converter.py +++ b/src/corva_unit_converter/converter.py @@ -76,9 +76,6 @@ def convert(self, unit_from: str, unit_to: str, # noqa: CCR001, CFQ004 result = value * origin["unit"]["to_anchor"] # for some units it's a simple shift (e.g. C to K) - if "anchor_shift" in destination["unit"]: - result += destination["unit"]["anchor_shift"] - if "anchor_shift" in origin["unit"]: result -= origin["unit"]["anchor_shift"] @@ -87,10 +84,15 @@ def convert(self, unit_from: str, unit_to: str, # noqa: CCR001, CFQ004 # Convert through transformation transform = measures[origin["measure"]]["_anchors"][origin["system"]].get("transform") if transform and callable(transform): - return transform(result) + result = transform(result) + # Convert through the anchor ratio result *= measures[origin["measure"]]["_anchors"][origin["system"]]["ratio"] + # Apply shift after transformation for F to K + if "anchor_shift" in destination["unit"]: + result += destination["unit"]["anchor_shift"] + # Convert to another unit inside the destination system return result / destination["unit"]["to_anchor"] diff --git a/src/corva_unit_converter/definitions/temperature.py b/src/corva_unit_converter/definitions/temperature.py index 90ee20b..4c6c5ec 100644 --- a/src/corva_unit_converter/definitions/temperature.py +++ b/src/corva_unit_converter/definitions/temperature.py @@ -45,11 +45,13 @@ "_anchors": { "metric": { "unit": "C", - "transform": lambda c: c / (5 / 9) + 32 + "transform": lambda c: c / (5 / 9) + 32, + "ratio": 1 }, "imperial": { "unit": "F", - "transform": lambda f: (f - 32) * (5 / 9) + "transform": lambda f: (f - 32) * (5 / 9), + "ratio": 1 } } } diff --git a/tests/test_temperature.py b/tests/test_temperature.py index cbecc42..ffe95b2 100644 --- a/tests/test_temperature.py +++ b/tests/test_temperature.py @@ -6,6 +6,8 @@ {"from": "F", "amount": 32, "to": "C", "expected": 0}, {"from": "K", "amount": 0, "to": "C", "expected": -273.15}, {"from": "C", "amount": 0, "to": "K", "expected": 273.15}, + {"from": "F", "amount": 32, "to": "K", "expected": 273.15}, + {"from": "K", "amount": 0, "to": "F", "expected": -459.67}, ]