123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- import { gameMethod } from "../common/gameMethod"
- import GameDataCenter from "../data/GameDataCenter"
- export default class GameMath {
- /**
- * 保留几位有效小数
- * @param decimals 小数
- * @param num 位数
- */
- static keep(decimals: number, num: number): number {
- // return Math.floor(decimals * Math.pow(10, num)) / Math.pow(10, num)
- // 原方法精度有偏差,这里精度多一位
- let val = Math.floor(decimals * Math.pow(10, num + 1))
- if (val % 10 > 0) {
- // 去余保证最后一位为0
- val = val - val % 10
- }
- return val / Math.pow(10, num + 1)
- }
- // static unit = 9999
- // /**
- // * 长数字简化
- // * @param num 原数字
- // * @param min 大于这个数才简化(要优化成位数并表示简化后长度)
- // * @param mathUnit 简化的单位
- // * @param unitLen 一个单位表示位数。如万表示4位k表示3位
- // */
- // static showNum(num: number, min: number = GameMath.unit, mathUnit: Array<string> = GameMath.mathUnit, unitLen: number = 4): string {
- // if (gameMethod.isEmpty(num)) {
- // return "0"
- // }
- // if (num > min) {
- // let len = Math.floor(Math.log10(num))
- // let unit = Math.floor(len / unitLen)
- // return this.keep(num / Math.pow(Math.pow(10, unitLen), unit), unitLen - 1 - len % unitLen) + mathUnit[unit - 1]
- // } else {
- // return num.toString()
- // }
- // }
- static mathUnit = ["万", "亿", "兆", "京", "垓", "杼"]//
- // static mathUnit = ["K", "M", "B", "T", "KT", "MT"]//["万", "亿", "兆", "京", "垓", "杼"]//
- static showNum(num: number, index: number = 0, MinNum: number = 10000): string {
- if (isNaN(num)) {
- return "0"
- }
- let _num = num
- let isNeedShow = num / MinNum
- num = num / 10000
- if (isNeedShow < 1) {
- let strList = _num.toString().split('.')
- let value = ""
- if (strList[1]) {
- let count = 5 - strList[0].length
- let digit = Math.min(count, 2)
- value = strList[1].slice(0, digit)
- for (let i = value.length - 1; i >= 0; i--) {
- const element = value[i];
- if (element == "0") {
- value = value.substring(0, i)
- } else {
- break
- }
- }
- }
- if (value.length > 0) {
- return strList[0] + "." + value + (index > 0 ? GameMath.mathUnit[index - 1] : "")
- } else {
- return strList[0] + (index > 0 ? GameMath.mathUnit[index - 1] : "")
- }
- } else {
- return GameMath.showNum(num, index + 1)
- }
- }
- /**
- * 判断一个数字有几位数
- * @param num 数字
- * @returns 位数
- */
- static getNumberOfDigits(num: number): number {
- if (num === 0) {
- return 1;
- }
- return Math.floor(Math.log10(Math.abs(num))) + 1;
- }
- static addZero(num: number): string {
- if (num >= 10) {
- return num.toString()
- }
- return '0' + num.toString()
- }
- /**
- * 获取0点
- */
- static getDay0(time: number): number {
- let date = new Date(time * 1000).setHours(0, 0, 0, 0);//获取今天0点0分0秒0毫秒
- return Math.floor(date / 1000);;
- }
- /**
- * 获取每日重置时间
- */
- static getRstTime(time: number) {
- return this.getDay0(time) + 86400
- }
- /** 获取当天某个时间的时间戳 */
- static getTodaySecond(h = 0, m = 0, s = 0) {
- var curTime = GameDataCenter.time && GameDataCenter.time.sevTime || 0;
- var time = new Date(curTime * 1000).setHours(h, m, s) / 1000;
- return time;
- }
- /**
- * 时间戳转换
- * @param time 时间戳
- * @param format 返回格式,支持自定义,但参数必须与formateArr里保持一致
- * 年Y 月M 日D 时h 分m 秒s
- */
- static formatTime(time: number, format: string) {
- let formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
- let returnArr = [];
- let date = new Date(time * 1000);
- returnArr.push(date.getFullYear());
- returnArr.push(this.formatNumber(date.getMonth() + 1));
- returnArr.push(this.formatNumber(date.getDate()));
- returnArr.push(this.formatNumber(date.getHours()));
- returnArr.push(this.formatNumber(date.getMinutes()));
- returnArr.push(this.formatNumber(date.getSeconds()));
- for (let i in returnArr) {
- format = format.replace(formateArr[i], returnArr[i]);
- }
- return format;
- }
- /**
- * 个位数十位补零
- * @param n 数值
- */
- static formatNumber(n: number) {
- let num = n.toString()
- return num[1] ? num : '0' + num
- }
- /**
- * 活跃度进度条
- * @param curScore 数值
- * @param scoreList 数值列表
- * @param nWidth 节点宽度
- */
- static fgetProgress(curScore: number, scoreList: number[], nWidth: number): number {
- let width: number = 0
- let index: number = 0
- for (let i = 0; i < scoreList.length; i++) {
- if (curScore <= scoreList[i]) {
- if (i > 0) {
- index = curScore - scoreList[i - 1]
- width += index * ((nWidth / scoreList.length) / (scoreList[i] - scoreList[i - 1]))
- } else {
- index = curScore
- width += index * ((nWidth / scoreList.length) / (scoreList[i]))
- }
- return width
- } else {
- width += scoreList[i] * ((nWidth / scoreList.length) / scoreList[i])
- }
- }
- return width
- }
- /**数组去重 */
- static unique<T>(arr: Array<T>): Array<T> {
- const res = new Map();
- return arr.filter((a) => !res.has(a) && res.set(a, 1))
- }
- /**传入某天零点时间戳 获取距离今天有几天 */
- public static getDistanceDays(sTime) {
- var zeroTime = this.getTodaySecond(0);//当天零点时间
- return Math.floor((zeroTime - sTime) / 86400) + 1;
- }
- /**
- * 格式化时间戳 获取 月/日 时:分:秒 2021-5-23 14:26:31
- * @param time 时间戳
- * @returns
- */
- public static formatTimeMDHMS(time) {
- let timeData = this.formatTimeSTamp(time * 1000);
- return `${timeData["month"]}/${timeData["day"]} ${timeData["hour"]}:${timeData["minute"]}:${timeData["second"]}`;
- }
- public static formatTimeSTamp(ts) {
- let timeData = {};
- var time = new Date(ts);
- timeData["year"] = this.getTimeAddSuffix(time.getFullYear())
- timeData["month"] = this.getTimeAddSuffix(time.getMonth() + 1)
- timeData["day"] = this.getTimeAddSuffix(time.getDate())
- timeData["hour"] = this.getTimeAddSuffix(time.getHours())
- timeData["minute"] = this.getTimeAddSuffix(time.getMinutes())
- timeData["second"] = this.getTimeAddSuffix(time.getSeconds())
- return timeData;
- }
- public static getTimeAddSuffix(time) {
- if (Number(time) < 10) {
- return "0" + time;
- }
- return time;
- }
- /**
- * 获取范围内的随机整数
- * @param min
- * @param max
- * @returns
- */
- public static getRandomNum(min, max) {
- return Math.floor(Math.random() * (max - min) + min)
- }
- /**
- * 获取范围内的随机数
- * @param min
- * @param max
- * @returns
- */
- public static getRandomFloor(min, max) {
- return Math.random() * (max - min) + min
- }
- /**
- * 保留小数点后不显示0
- * @param num
- * @param divNum
- * @param fixedNum
- * @returns
- */
- public static toFixNumFormat(num: number, divisor: number, fixedNum: number) {
- // 将数值除以给定的除数
- let result = num / divisor;
- let intPart = Math.floor(result);
- let hasFractionalPart = result !== intPart;
- if (hasFractionalPart) {
- // 如果有小数部分,则保留两位小数
- let formatted = result.toFixed(fixedNum);
- // 分割字符串为整数部分和小数部分
- let [integerPart, decimalPart] = formatted.split('.');
- // 去除小数部分末尾的0
- let trimmedDecimalPart = decimalPart.replace(/0+$/, '');
- // 如果小数部分被完全去除(即原小数部分只包含0),则返回整数部分
- if (trimmedDecimalPart === '') {
- return integerPart;
- } else {
- // 否则,将整数部分和修剪后的小数部分重新组合
- return integerPart + '.' + trimmedDecimalPart;
- }
- } else {
- // 如果没有小数部分,则直接返回整数部分
- return intPart.toString();
- }
- }
- }
|