fix: round website rates to two decimals

This commit is contained in:
SmartUp Developer
2026-06-30 11:26:48 +08:00
parent a914d3d222
commit 2d1dcb8f9f
6 changed files with 88 additions and 26 deletions
+18 -1
View File
@@ -1,7 +1,7 @@
"""Shared numeric formatting utilities."""
from __future__ import annotations
from decimal import Decimal, InvalidOperation
from decimal import Decimal, InvalidOperation, ROUND_HALF_UP
from typing import Any
@@ -23,3 +23,20 @@ def decimal_string(value: Any) -> str:
if n == n.to_integral():
return str(n.quantize(Decimal("1")))
return format(n, "f")
def fixed_decimal_string(value: Any, places: int = 2) -> str:
"""Format a numeric value with a fixed number of decimal places."""
if value is None or value == "":
return ""
try:
d = Decimal(str(value))
except (InvalidOperation, ValueError):
return str(value)
quantum = Decimal("1").scaleb(-places)
return format(d.quantize(quantum, rounding=ROUND_HALF_UP), f".{places}f")
def fixed_decimal_number(value: Any, places: int = 2) -> float:
"""Round a numeric value to a fixed precision for JSON number payloads."""
return float(fixed_decimal_string(value, places))