mirror of
https://gitee.com/dgflash/oops-plugin-framework.git
synced 2026-05-08 03:16:49 +08:00
138 lines
4.5 KiB
TypeScript
138 lines
4.5 KiB
TypeScript
/**
|
|
* 数值格式化函数, 通过语义解析自动设置值的范围
|
|
* //整数
|
|
* 1:def(0)//显示一个默认值
|
|
*/
|
|
class StringFormat {
|
|
deal(value: number | string, format: string): string {
|
|
if (format === '') return value as string;
|
|
|
|
format = format.toLowerCase().trim(); // 不区分大小
|
|
let match_func = format.match(/^[a-z|A-Z]+/gi); // 匹配到 format 中的 函数名
|
|
let match_num = format.match(/\d+$/gi); // 匹配到 format 中的参数
|
|
let func: string = '';
|
|
let num: number = 0;
|
|
let res: number | string = '';
|
|
|
|
if (match_func) func = match_func[0];
|
|
if (match_num) num = parseInt(match_num[0]);
|
|
|
|
if (typeof value == 'number') {
|
|
switch (func) {
|
|
case 'int': res = this.int(value); break;
|
|
case 'fix': res = this.fix(value, num); break;
|
|
case 'kmbt': res = this.kmbt(value); break;
|
|
case 'per': res = this.per(value, num); break;
|
|
case 'sep': res = this.sep(value); break;
|
|
|
|
case 'stamp': res = this.time_stamp(value); break;
|
|
case 'ms': res = this.time_ms(value); break;
|
|
case 'hms': res = this.time_hms(value); break;
|
|
case 'hmss': res = this.time_hmss(value); break;
|
|
|
|
default: break;
|
|
}
|
|
}
|
|
else {
|
|
switch (func) {
|
|
case 'limit': res = this.limit(value, num); break;
|
|
default:
|
|
break;
|
|
}
|
|
res = value;
|
|
}
|
|
|
|
return res as string;
|
|
}
|
|
|
|
/** 将数字按分号显示 */
|
|
private sep(value: number) {
|
|
let num = Math.round(value).toString();
|
|
return num.replace(new RegExp('(\\d)(?=(\\d{3})+$)', 'ig'), "$1,");
|
|
}
|
|
|
|
/** 将数字按分显示 00:00 显示 (分:秒) */
|
|
private time_ms(value: number) {
|
|
return new Date(value).format('mm:ss');
|
|
}
|
|
|
|
/** 将数字按秒显示 00:00:00 显示 (时:分:秒) */
|
|
private time_hms(value: number) {
|
|
return new Date(value).format('hh:mm:ss');
|
|
}
|
|
|
|
/** 将数字按 0:00:00:000 显示 (时:分:秒:毫秒) */
|
|
private time_hmss(value: number) {
|
|
return new Date(value).format('hh:mm:ss:ms');
|
|
}
|
|
|
|
/** 将时间戳显示为详细的内容 */
|
|
private time_stamp(value: number) {
|
|
return new Date(value).format('yy-mm-dd hh:mm:ss');
|
|
}
|
|
|
|
/** [value:int] 将取值0~1 变成 1~100,可以指定修饰的小数位数 */
|
|
private per(value: number, fd: number) {
|
|
let r = value * 100;
|
|
return r.toFixed(fd);
|
|
}
|
|
|
|
/** [value:int] 将取值变成整数 */
|
|
private int(value: number) {
|
|
return Math.round(value);
|
|
}
|
|
|
|
/** [value:fix2]数值转换为小数 */
|
|
private fix(value: number, fd: number) {
|
|
return value.toFixed(fd)
|
|
}
|
|
|
|
/** [value:limit3]字符串长度限制 */
|
|
private limit(value: string, count: number) {
|
|
return value.substring(0, count);
|
|
}
|
|
|
|
/** 将数字缩短显示为KMBT单位 大写,目前只支持英文 */
|
|
private kmbt(value: number, lang: string = 'en') {
|
|
//10^4=万, 10^8=亿,10^12=兆,10^16=京,
|
|
let counts = [1000, 1000000, 1000000000, 1000000000000];
|
|
let units = ['', 'K', 'M', 'B', 'T'];
|
|
|
|
switch (lang) {
|
|
case 'zh':
|
|
//10^4=万, 10^8=亿,10^12=兆,10^16=京,
|
|
counts = [10000, 100000000, 1000000000000, 10000000000000000];
|
|
units = ['', '万', '亿', '兆', '京'];
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return this.compressUnit(value, counts, units, 2);
|
|
}
|
|
|
|
/** 压缩任意单位的数字,后缀加上单位文字 */
|
|
private compressUnit(value: any, valueArr: number[], unitArr: string[], fixNum: number = 2): string {
|
|
let counts = valueArr;
|
|
let units = unitArr;
|
|
let res: string = "";
|
|
let index: number;
|
|
for (index = 0; index < counts.length; index++) {
|
|
const e = counts[index];
|
|
if (value < e) {
|
|
if (index > 0) {
|
|
res = (value / counts[index - 1]).toFixed(fixNum);
|
|
}
|
|
else {
|
|
res = value.toFixed(0);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
return res + units[index];
|
|
}
|
|
}
|
|
|
|
/** 格式化处理函数 */
|
|
export let StringFormatFunction = new StringFormat(); |