fix: round website rates to two decimals
This commit is contained in:
@@ -7,7 +7,7 @@ from urllib.parse import quote
|
||||
|
||||
import httpx
|
||||
|
||||
from app.utils.number import decimal_string
|
||||
from app.utils.number import fixed_decimal_number, fixed_decimal_string
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -67,7 +67,7 @@ def calculate_target_rate(values: list[Any], percent: Any = 0, algorithm: str =
|
||||
pct = Decimal(str(percent or 0))
|
||||
if pct < 0:
|
||||
raise WebsiteError("百分比不能为负数")
|
||||
return (base * (Decimal("1") + pct / Decimal("100"))).quantize(Decimal("0.0001"), rounding=ROUND_HALF_UP)
|
||||
return (base * (Decimal("1") + pct / Decimal("100"))).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
|
||||
|
||||
|
||||
def _unwrap_data(value: Any) -> Any:
|
||||
@@ -122,7 +122,7 @@ def normalize_groups(value: Any) -> list[dict[str, Any]]:
|
||||
groups.append({
|
||||
"id": str(gid),
|
||||
"name": str(name),
|
||||
"rate_multiplier": decimal_string(rate) if rate is not None else None,
|
||||
"rate_multiplier": fixed_decimal_string(rate, 2) if rate is not None else None,
|
||||
"raw": item,
|
||||
})
|
||||
return groups
|
||||
@@ -209,9 +209,9 @@ class Sub2ApiWebsiteClient:
|
||||
msg = str(last_error) if last_error else "拉取分组失败"
|
||||
raise WebsiteError(f"{msg}(尝试接口:{'、'.join(tried_paths)})")
|
||||
|
||||
def update_group_rate(self, endpoint_template: str, group_id: str, rate: Decimal) -> Any:
|
||||
def update_group_rate(self, endpoint_template: str, group_id: str, rate: Any) -> Any:
|
||||
path = endpoint_template.replace("{id}", quote(group_id, safe=""))
|
||||
return self._request("PUT", path, {"rate_multiplier": float(rate)})
|
||||
return self._request("PUT", path, {"rate_multiplier": fixed_decimal_number(rate, 2)})
|
||||
|
||||
def create_group(self, body: dict[str, Any], endpoint: str = "/groups") -> dict[str, Any]:
|
||||
resp = self._request("POST", endpoint, body)
|
||||
|
||||
Reference in New Issue
Block a user