feat: Improve float parsing logic for enhanced accuracy in numeric conversions
Deploy Development / deploy (push) Successful in 56s
Build Test / pytest-backend (push) Successful in 4s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 16s

- Updated the `_parse_float_auto` function in `type_converter.py` to better handle various decimal and thousand separators, particularly for cases with long decimal parts from sources like Apple Health.
- Enhanced the logic for splitting and processing numeric strings to ensure correct interpretation of values, including edge cases with multiple separators.
- Added handling for cases where numeric strings may contain both commas and periods, improving overall robustness in float parsing.

These changes enhance the accuracy of numeric conversions, ensuring more reliable data processing across the application.
This commit is contained in:
Lars committed 2026-04-12 07:28:24 +02:00
1 parent d7cefdd9e9
commit 90a27846ca
2 files changed
+62 -8

No files matched your search

+19
View File
@@ -81,6 +81,25 @@ def test_convert_kcal_via_source_unit_kj():
assert abs(k - 1000.0) < 0.05
def test_apple_health_long_decimal_dot_preserved():
"""Apple: Mittel-HF u. Energie mit vielen Nachkommastellen; Punkt ist Dezimaltrenner."""
hr_spec = {"type": "int", "flexible": True}
r = convert_value("96.8749374730629", "hr_avg", hr_spec, module="activity")
assert r == 97
rest_spec = {"type": "float", "decimal_separator": ".", "flexible": True, "source_unit": "kj"}
kcal = convert_value("596.6689047323086", "kcal_resting", rest_spec, module="activity")
assert 140.0 < kcal < 145.0
def test_parse_float_auto_us_thousands_comma():
"""«12,345» ohne Dezimalpunkt weiter als Tausender möglich."""
v = convert_value("12345", "x", {"type": "float", "decimal_separator": "auto"})
assert v == 12345.0
v2 = convert_value("12,345", "x", {"type": "float", "decimal_separator": "auto"})
assert abs(v2 - 12345.0) < 0.01
def test_convert_protein_kg_to_g():
spec = {"type": "float", "source_unit": "kg", "decimal_separator": "."}
g = convert_value("0.1", "protein_g", spec, module="nutrition")