1212
This commit is contained in:
38
Web/src/utility/GameTool.ts
Normal file
38
Web/src/utility/GameTool.ts
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user