123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- /**
- * 用于判断浮点数是否相等
- * @param l
- * @param r
- */
- export function isEqualFloat(l: number, r: number) {
- const s = Math.abs(l - r);
- return s < 0.0000001;
- }
- export function todayDateStr() {
- const now = new Date()
- return `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`
- }
- export function formaterYmdDate(date: Date) {
- return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
- }
- export function formatTime() {
- const now = new Date()
- return `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()} ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`
- }
- export function fixedFloat(l: number) {
- const value = Math.round(l * 10);
- return value / 10;
- }
- export function fixed2Float(l: number) {
- const value = Math.round(l * 100);
- return value / 100;
- }
- export function fixed3Float(l: number) {
- const value = Math.round(l * 1000);
- return value / 1000;
- }
- export function formatterCoin(coin: number) {
- if (coin >= 1000 && coin < 1000000) {
- return `${fixedFloat(coin / 1000)}K`
- }
- if (coin >= 1000000) {
- return `${fixedFloat(coin / 1000000)}M`
- }
- return `${coin}`
- }
- export function randomNumberString(length: number) {
- let str = '';
- for (let i = 0; i < length; i++) {
- str += Math.floor(Math.random() * 10);
- }
- return str;
- }
- export function paging<T>(page: number = 1, size: number = 10, list: T[]) {
- const start = (page - 1) * size;
- let end = start + size;
- if (list.length < start) {
- return [];
- }
- if (list.length < end) {
- end = list.length;
- }
- return list.slice(start, end);
- }
- export function nTCN(num: number) {
- const texts = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
- const str = `${num}`
- let result = ``
- for (let index = 0; index < str.length; index++) {
- result += texts[Number(str.charAt(index))]
- }
- return result
- }
- export function shuffle<T>(arr: T[]) {
- let newArr = arr.concat();
- let j, temp;
- for (let i = arr.length; i > 0; i--) {
- j = Math.floor(Math.random() * i);
- temp = newArr[i - 1];
- newArr[i - 1] = newArr[j];
- newArr[j] = temp;
- }
- return newArr;
- }
- export function unique<T>(arr: T[]) {
- return Array.from(new Set(arr));
- }
- //验证字符串是否是数字
- export function isNumber(theObj: any) {
- return !isNaN(parseFloat(theObj)) && isFinite(theObj)
- }
- export function undefinedValue<T>(v: T, defaultValue: T) {
- return v == undefined || v == null ? defaultValue : v
- }
- export function formatToTwoDigits(number: number): string {
- if (number < 10) {
- return '0' + number;
- }
- return '' + number;
- }
- export function secondsToTimeFormat(seconds: number, needHour = false): string {
- if (seconds < 0) {
- return ``
- }
- const hours = Math.floor(seconds / 3600);
- const minutes = Math.floor((seconds % 3600) / 60);
- const remainingSeconds = Math.floor(seconds % 60);
- const formattedHours = formatToTwoDigits(hours);
- const formattedMinutes = formatToTwoDigits(minutes);
- const formattedSeconds = formatToTwoDigits(remainingSeconds);
- if (hours == 0 && !needHour) {
- return `${formattedMinutes}:${formattedSeconds}`;
- }
- return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
- }
|