You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
333 lines
12 KiB
333 lines
12 KiB
/**字符串工具 */
|
|
export default class StringUtils{
|
|
public static GetAttackXi(AttackBase: number, Level: number, xishu: number): number {
|
|
for (let i = 0; i < Level - 1; i++) {
|
|
AttackBase *= xishu;
|
|
}
|
|
return AttackBase;
|
|
}
|
|
/**字符串拆解 */
|
|
public static GetStrByArray(str: string, _: string = "|") {
|
|
let Temp = str.split(_);
|
|
let TempArray:number[] = [];
|
|
for (let i = 0; i < Temp.length; i++) {
|
|
TempArray.push(Number(Temp[i]));
|
|
}
|
|
return TempArray;
|
|
}
|
|
public static GetBuildingStrByArray(str: string, _: string = ":"): string[] {
|
|
if (str == null) {
|
|
return [];
|
|
}
|
|
let Temp = str.split(_);
|
|
return Temp;
|
|
}
|
|
|
|
public static RoleSpeed: number = 1;
|
|
public static GetFloat2ByString(value: number,isZh:boolean = true,_z:string = "元") {
|
|
let TempValue = 0;
|
|
let _s = "";
|
|
let TempStr = "";
|
|
if (value >= 10000) {
|
|
_s = isZh ? "万" : "M";
|
|
TempValue = value / 10000;
|
|
} else {
|
|
TempValue = value;
|
|
}
|
|
TempValue *= 100;
|
|
TempValue = Math.floor(TempValue);
|
|
TempValue = TempValue / 100;
|
|
TempStr = TempValue + _s + _z;
|
|
return TempStr;
|
|
}
|
|
|
|
// static ZhMonylist = [
|
|
// [
|
|
// "清",
|
|
// "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
|
|
// ],
|
|
// [
|
|
// "明",
|
|
// "1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
|
|
// ],
|
|
// [
|
|
// "宋",
|
|
// "100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
|
|
// ],
|
|
// [
|
|
// "唐",
|
|
// "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
|
|
// ],
|
|
// [
|
|
// "隋",
|
|
// "1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
|
|
// ],
|
|
// [
|
|
// "晋",
|
|
// "100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
|
|
// ],
|
|
// [
|
|
// "吴",
|
|
// "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
|
|
// ],
|
|
// [
|
|
// "蜀",
|
|
// "1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
|
|
// ],
|
|
// ["魏", "100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"],
|
|
// ["汉", "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"],
|
|
// ["秦", "1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"],
|
|
// ["周", "100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"],
|
|
// ["商", "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"],
|
|
// ["夏", "1000000000000000000000000000000000000000000000000000000000000000000000000000000000000"],
|
|
// ["古", "100000000000000000000000000000000000000000000000000000000000000000000000000000000"],
|
|
// ["全", "10000000000000000000000000000000000000000000000000000000000000000000000000000"],
|
|
// ["大", "1000000000000000000000000000000000000000000000000000000000000000000000000"],
|
|
// ["无", "100000000000000000000000000000000000000000000000000000000000000000000"],
|
|
// ["不", "10000000000000000000000000000000000000000000000000000000000000000"],
|
|
// ["那", "1000000000000000000000000000000000000000000000000000000000000"],
|
|
// ["阿", "100000000000000000000000000000000000000000000000000000000"],
|
|
// ["恒", "10000000000000000000000000000000000000000000000000000"],
|
|
// ["极", "1000000000000000000000000000000000000000000000000"],
|
|
// ["载", "100000000000000000000000000000000000000000000"],
|
|
// ["正", "10000000000000000000000000000000000000000"],
|
|
// ["涧", "1000000000000000000000000000000000000"],
|
|
// ["沟", "100000000000000000000000000000000"],
|
|
// ["穰", "10000000000000000000000000000"],
|
|
// ["秭", "1000000000000000000000000"],
|
|
// ["垓", "100000000000000000000"],
|
|
// ["京", "10000000000000000"],
|
|
// ["兆", "1000000000000"],
|
|
// ["亿", "100000000"],
|
|
// ["万", "10000"],
|
|
// ["", "1"]
|
|
// ].reverse()
|
|
|
|
|
|
|
|
// public static MoneyConvert(value: number,Fixed:number = 2) {
|
|
|
|
// if (value == 0) {
|
|
// return 0 + this.ZhMonylist[0][0];
|
|
// }
|
|
// let TempValue = value;
|
|
// let TempStr = "";
|
|
// //8let ValueLen = value.toString().length;
|
|
// let tIndex: number = 0;
|
|
// for (let i = 0; i < this.ZhMonylist.length; i++) {
|
|
// let data = this.ZhMonylist[i];
|
|
// if (value <= Number(data[1])) {
|
|
// tIndex = i;
|
|
// break;
|
|
// }
|
|
// }
|
|
// for (let i = tIndex; i >= 0; i--) {
|
|
// const list = this.ZhMonylist[i];
|
|
// let mantissa = list[0];
|
|
// let tNumber = Number(list[1]);
|
|
// if (TempValue >= tNumber) {
|
|
// let offset = Math.floor(TempValue / tNumber);
|
|
// TempStr += offset + mantissa;
|
|
// TempValue -= offset * tNumber;
|
|
// Fixed--;
|
|
// if (Fixed <= 0) {
|
|
// break;
|
|
// }
|
|
// }
|
|
// }
|
|
// return TempStr;
|
|
// }
|
|
|
|
|
|
static EnMonylist = [
|
|
["b", "1000000000"],
|
|
["m", "1000000"],
|
|
["k", "1000"],
|
|
["", "1"]
|
|
].reverse()
|
|
|
|
/**伤害文本显示转换 */
|
|
static HurtConvert(value: number, fix: number = 2) {
|
|
|
|
let TempStr = "";
|
|
if (value > 1000000000) {
|
|
TempStr = (value/1000000000).toFixed(fix)+"b"
|
|
} else if (value > 1000000) {
|
|
TempStr = (value/1000000).toFixed(fix)+"m"
|
|
} else if (value > 1000) {
|
|
TempStr = (value/1000).toFixed(fix)+"k"
|
|
}else{
|
|
TempStr = value.toString();
|
|
}
|
|
return TempStr.replace(".00", "");
|
|
}
|
|
|
|
/**
|
|
* 获得UUID
|
|
* @returns
|
|
*/
|
|
static getUUID(): string {
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
|
return v.toString(16);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 将传入的阿拉伯数字转化成汉语的读取方式,返回汉语
|
|
* @param value 需要转换的数字
|
|
* @param withUnits 是否添加数位单位(如:十、百、千、万等),默认为true
|
|
* @returns 转换后的中文表示
|
|
*/
|
|
static ChineseNumConvert(value: number, withUnits: boolean = true): string {
|
|
console.log("进行转换");
|
|
if (value === 0) {
|
|
return "零";
|
|
}
|
|
|
|
const digits = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
|
|
const units = ["", "十", "百", "千"];
|
|
const bigUnits = ["", "万", "亿"];
|
|
|
|
// 处理负数
|
|
let isNegative = false;
|
|
if (value < 0) {
|
|
isNegative = true;
|
|
value = Math.abs(value);
|
|
}
|
|
|
|
// 不带单位的简单转换
|
|
if (!withUnits) {
|
|
const str = value.toString();
|
|
let result = "";
|
|
for (let i = 0; i < str.length; i++) {
|
|
result += digits[parseInt(str[i])];
|
|
}
|
|
return isNegative ? "负" + result : result;
|
|
}
|
|
|
|
// 带单位的转换
|
|
function convertSection(num: number): string {
|
|
let result = "";
|
|
let needZero = false;
|
|
|
|
for (let i = 0; i < 4 && num > 0; i++) {
|
|
const digit = num % 10;
|
|
|
|
if (digit === 0) {
|
|
if (result !== "") {
|
|
needZero = true;
|
|
}
|
|
} else {
|
|
if (needZero) {
|
|
result = digits[digit] + units[i] + "零" + result;
|
|
needZero = false;
|
|
} else {
|
|
result = digits[digit] + units[i] + result;
|
|
}
|
|
}
|
|
|
|
num = Math.floor(num / 10);
|
|
}
|
|
|
|
// 移除末尾的零
|
|
return result.replace(/零+$/, "");
|
|
}
|
|
|
|
if (value < 10) {
|
|
return isNegative ? "负" + digits[value] : digits[value];
|
|
}
|
|
|
|
let result = "";
|
|
let unitIndex = 0;
|
|
|
|
while (value > 0) {
|
|
const section = value % 10000;
|
|
const sectionText = convertSection(section);
|
|
|
|
if (sectionText !== "") {
|
|
if (unitIndex > 0) {
|
|
result = sectionText + bigUnits[unitIndex] + result;
|
|
} else {
|
|
result = sectionText + result;
|
|
}
|
|
} else if (result !== "" && unitIndex > 0) {
|
|
// 只有当已有高位数字时才添加零
|
|
if (!result.startsWith("零")) {
|
|
result = "零" + result;
|
|
}
|
|
}
|
|
|
|
value = Math.floor(value / 10000);
|
|
unitIndex++;
|
|
}
|
|
|
|
// 特殊处理10-19的数字(省略开头的"一")
|
|
if (result.startsWith("一十")) {
|
|
result = result.substring(1);
|
|
}
|
|
|
|
// 清理多余的零
|
|
result = result.replace(/零+/g, "零");
|
|
|
|
return isNegative ? "负" + result : result;
|
|
}
|
|
|
|
/**
|
|
* 将大数值转换为带单位的格式显示(万/亿)
|
|
* @param value 需要转换的数值
|
|
* @param decimalPlaces 小数点位数
|
|
* @returns 转换后的字符串格式
|
|
*/
|
|
static formatLargeNumber(value: number, decimalPlaces: number = 2): string {
|
|
// 处理无效数值
|
|
if (typeof value !== 'number' || !isFinite(value)) {
|
|
return '0';
|
|
}
|
|
|
|
if (value <= 10000){
|
|
return value.toString();
|
|
}
|
|
|
|
// 记录是否为负数
|
|
const isNegative = value < 0;
|
|
// 取绝对值进行计算
|
|
const absValue = Math.abs(value);
|
|
|
|
let result: string;
|
|
|
|
// 小于一万直接返回原数值的字符串形式
|
|
if (absValue < 10000) {
|
|
result = absValue.toString();
|
|
}
|
|
// 处理万级数值 (10000 - 99999999)
|
|
else if (absValue < 100000000) {
|
|
const wanValue = absValue / 10000;
|
|
const formatted = wanValue.toFixed(decimalPlaces);
|
|
result = formatted.endsWith('.00') ? `${Math.floor(wanValue)}万` : `${formatted}万`;
|
|
}
|
|
// 处理亿级数值 (>= 100000000)
|
|
else {
|
|
const yiValue = absValue / 100000000;
|
|
const formatted = yiValue.toFixed(decimalPlaces);
|
|
result = formatted.endsWith('.00') ? `${Math.floor(yiValue)}亿` : `${formatted}亿`;
|
|
}
|
|
|
|
// 如果是负数,在结果前加上负号
|
|
return isNegative ? `-${result}` : result;
|
|
}
|
|
|
|
/**
|
|
* 字符串格式化方法,类似C#的string.Format
|
|
* 使用示例: StringUtils.format("御器额外伤害+{0}%; 获得御器经验额外+{1}%", prop1, prop2)
|
|
* @param str 包含{0}、{1}等占位符的字符串
|
|
* @param args 要替换的参数列表
|
|
* @returns 格式化后的字符串
|
|
*/
|
|
static format(str: string, ...args: any[]): string {
|
|
return str.replace(/{(\d+)}/g, (match: string, index: string) => {
|
|
return typeof args[index] !== 'undefined' ? args[index].toString() : match;
|
|
});
|
|
}
|
|
|
|
}
|