This commit is contained in:
Putoo
2026-05-26 18:38:55 +08:00
parent 61cf882b66
commit 6b7193b4c0
33 changed files with 1021 additions and 42 deletions

View File

@@ -0,0 +1,38 @@
export class GameTool {
/**
* 静态方法:将总铜数转换为 金/银/铜 字符串
* @param totalCopper 总铜数量
* @returns 格式化字符串1金 234银 567铜
*/
public static FormatCopper(totalCopper: number | string): string {
// 安全转为数字并取整(货币无小数)
let copper = Math.floor(Number(totalCopper) || 0);
// 处理负数
const isNegative = copper < 0;
copper = Math.abs(copper);
// 单位换算
const copperUnit = copper % 1000;
const totalSilver = Math.floor(copper / 1000);
const silverUnit = totalSilver % 1000;
const goldUnit = Math.floor(totalSilver / 1000);
// 拼接非0单位
const parts: string[] = [];
if (goldUnit > 0) parts.push(`${goldUnit}`);
if (silverUnit > 0) parts.push(`${silverUnit}`);
if (copperUnit > 0) parts.push(`${copperUnit}`);
// 空值显示 0铜
let result = parts.length > 0 ? parts.join(' ') : '0铜';
// 恢复负号
if (isNegative) {
result = `-${result}`;
}
return result;
}
}