moneyFormatWithUnit method
金额格式化(带单位)。
输入单位为分(fen):方法将分字符串除以 100 转为元。
当 autoMoneyUnit 为 true 且金额 >= 10000 元(即 >= 1000000 分)时,
自动折算为「万」单位输出,例如 12345678 分 → "12.35万"。
走 Decimal 运算(与 formatMoney 同策略),避免大额输入时
double 精度丢失;Decimal 无法解析的异形输入(如科学计数法)
回退旧 double 路径保持行为不变。
Implementation
String moneyFormatWithUnit(bool autoMoneyUnit) {
final s = this;
if (s == null || s.isEmpty) return '0.00';
final dec = Decimal.tryParse(s);
if (dec != null) {
if (autoMoneyUnit && dec >= Decimal.fromInt(1000000)) {
return '${dec.shift(-6).toStringAsFixed(2)}万';
}
return dec.shift(-2).toStringAsFixed(2);
}
// 回退:异形输入走旧 double 路径(失败按 0.00 处理)。
final money = double.tryParse(s) ?? 0.0;
if (autoMoneyUnit && money >= 1000000) {
return '${(money / 1000000).toStringAsFixed(2)}万';
}
return (money / 100).toStringAsFixed(2);
}