Files
ryan f8e74738cc feat(价格展示): 避免低价资产被格式化丢失为 0.00
Motivation:
交易通知等展示场景中,低价资产(如 0.00000059)按保留两位小数的格式渲染会被截断为 0.00,用户无法读取真实价格;需要在不改变存量数值类型和既有模板写法的前提下,保证任意精度价格都能正确展示。

Changes:

* 新增价格展示格式化能力:数值不小于 1 时保留两位小数,小于 1 时输出最短精确值
* 新增 Decimal 类型实现自定义格式化,使模板中的浮点值不再坍缩为 0.00 且不产生科学计数法
* 新增模板数据渲染期数值包装能力,不修改原始数据,保证条件判断与既有模板语法兼容
* 补充价格格式化、精度保留及数据不可变性的单元测试
2026-08-23 00:17:43 +08:00

53 lines
1.2 KiB
Go

package display
import (
"fmt"
"testing"
)
func TestFormatPrice(t *testing.T) {
cases := []struct {
in float64
want string
}{
{0, "0.00"},
{1898.76, "1898.76"},
{1, "1.00"},
{0.00000059, "0.00000059"},
{-0.00000059, "-0.00000059"},
{0.5, "0.5"},
}
for _, tc := range cases {
if got := FormatPrice(tc.in); got != tc.want {
t.Errorf("FormatPrice(%v)=%q want %q", tc.in, got, tc.want)
}
}
}
func TestDecimalPrintfKeepsTinyPrice(t *testing.T) {
d := Decimal(0.00000059)
if got := fmt.Sprintf("%.2f", d); got != "0.00000059" {
t.Fatalf("%%.2f=%q", got)
}
if got := fmt.Sprintf("%v", d); got != "0.00000059" {
t.Fatalf("%%v=%q", got)
}
if got := fmt.Sprintf("%.2f", Decimal(1898.76)); got != "1898.76" {
t.Fatalf("eth %%.2f=%q", got)
}
if got := fmt.Sprintf("%.2f", Decimal(1000000000)); got != "1000000000.00" {
t.Fatalf("qty %%.2f=%q", got)
}
}
func TestWrapMapDoesNotMutate(t *testing.T) {
in := map[string]interface{}{"price": 0.00000059}
out := WrapMap(in)
if _, ok := in["price"].(float64); !ok {
t.Fatalf("input mutated: %T", in["price"])
}
if _, ok := out["price"].(Decimal); !ok {
t.Fatalf("wrapped type=%T", out["price"])
}
}