美文网首页
2024-10-23 bigNumber常用的处理工具类

2024-10-23 bigNumber常用的处理工具类

作者: gdlooker | 来源:发表于2024-10-22 11:25 被阅读0次

示例代码如下:

import BigNumber from 'bignumber.js';

/**
 * a + b 
 */
export const bigPlus = (a: number | string, b: number | string) => {
    return new BigNumber(a).plus(new BigNumber(b)).toFixed()
}
export const bigNumberPlus = (a: number | string, b: number | string) => {
    return new BigNumber(a).plus(new BigNumber(b))
}
export const bigMinus = (a: number | string, b: number | string) => {
    return new BigNumber(a).minus(new BigNumber(b)).toFixed()
}
export const bigMult = (a: number | string, b: number | string) => {
    return new BigNumber(a).multipliedBy(new BigNumber(b)).toFixed()
}

export const bigDiv = (a: number | string, b: number | string) => {
    return new BigNumber(a).div(new BigNumber(b)).toFixed()
}
export const bigDivGetInteger = (a: number | string, b: number | string) => {
    return new BigNumber(a).div(new BigNumber(b)).decimalPlaces(0, BigNumber.ROUND_DOWN).toFixed()
}

//a是否大于b
export const isGt = (a: number | string, b: number | string) => {
    return new BigNumber(a).gt(new BigNumber(b))
}
//是否大于等于
export const isGtEqualTo = (a: number | string, b: number | string) => {
    return new BigNumber(a).isGreaterThanOrEqualTo(new BigNumber(b))
}
//a是否小于b
export const isLess = (a: number | string, b: number | string) => {
    return new BigNumber(a).isLessThan(new BigNumber(b))
}
//a是否小于等于b
export const isLessEqualTo = (a: number | string, b: number | string) => {
    return new BigNumber(a).isLessThanOrEqualTo(new BigNumber(b))
}
//是否等于
export const isEqualTo = (a: number | string, b: number | string) => {
    return new BigNumber(a).isEqualTo(new BigNumber(b))
}

// 保留n位小数但不进行四舍五入
export const parseNumber = (str: string | number, length: number) => {
    const result = new BigNumber(str).decimalPlaces(length, BigNumber.ROUND_DOWN).toFixed()
    return result;
}
//输入框输入处理  
export const inputFormatNumber = (num: string, n = 0) => {
    if (Number.isNaN(num)) {
        num = ''
        return num
    }
    if (!num) {
        num = ''
        return num
    }
    const t: string = num
    const hasDot = t.includes('.')
    let intSeg = hasDot ? t.split('.')[0] : t
    const dotSeg = hasDot ? t.split('.')[1] : ''
    //intSeg.split('').reverse().join('').replace(/[\d]{3}/g, (a) => a + ',').split('').reverse().join('')
    //如果整数部分  第一位是0 
    if (intSeg.substring(0, 1) === '0' && intSeg.substring(1, 2) !== '.') {
        //console.log("第二位必须为小数点")
        if (intSeg.length >= 2) {
            intSeg = intSeg.substring(1, 2)   // 06 取6
        } else {
            intSeg = '0'  //第二位必须为小数点  //如果第一位跟第二位都为0 直接默认为0
        }
    }
    let formattedIntSeg = ''
    formattedIntSeg = [intSeg,
        dotSeg.substring(0, n)].join('.')
    if (!hasDot) {
        formattedIntSeg = formattedIntSeg.replace(/^,/, '').replace(/\.$/, '')
    }
    return formattedIntSeg
}

相关文章

网友评论

      本文标题:2024-10-23 bigNumber常用的处理工具类

      本文链接:https://www.haomeiwen.com/subject/vdcrdjtx.html