Format trade prices to 3 decimals and round on ingest.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from flask import Flask
|
||||
|
||||
from app.config import Config
|
||||
@@ -8,11 +10,33 @@ from app.db import init_db, seed_if_empty
|
||||
from app.routes import register_routes
|
||||
|
||||
|
||||
def _fmt_price(value: Any, digits: int = 3) -> str:
|
||||
"""价格展示:固定小数位,避免浮点尾巴。"""
|
||||
if value is None or value == "":
|
||||
return "—"
|
||||
try:
|
||||
return f"{float(value):.{int(digits)}f}"
|
||||
except (TypeError, ValueError):
|
||||
return str(value)
|
||||
|
||||
|
||||
def _fmt_money(value: Any, digits: int = 2) -> str:
|
||||
if value is None or value == "":
|
||||
return "—"
|
||||
try:
|
||||
return f"{float(value):.{int(digits)}f}"
|
||||
except (TypeError, ValueError):
|
||||
return str(value)
|
||||
|
||||
|
||||
def create_app() -> Flask:
|
||||
app = Flask(__name__, template_folder="templates", static_folder="static")
|
||||
app.config.from_object(Config)
|
||||
app.secret_key = Config.SECRET_KEY
|
||||
|
||||
app.jinja_env.filters["fmt_price"] = _fmt_price
|
||||
app.jinja_env.filters["fmt_money"] = _fmt_money
|
||||
|
||||
init_db()
|
||||
seed_if_empty()
|
||||
register_routes(app)
|
||||
|
||||
Reference in New Issue
Block a user