2024-10-23 bigNumber常用的處理工具類

示例代碼如下:

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位小數(shù)但不進(jì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('')
    //如果整數(shù)部分  第一位是0 
    if (intSeg.substring(0, 1) === '0' && intSeg.substring(1, 2) !== '.') {
        //console.log("第二位必須為小數(shù)點(diǎn)")
        if (intSeg.length >= 2) {
            intSeg = intSeg.substring(1, 2)   // 06 取6
        } else {
            intSeg = '0'  //第二位必須為小數(shù)點(diǎn)  //如果第一位跟第二位都為0 直接默認(rèn)為0
        }
    }
    let formattedIntSeg = ''
    formattedIntSeg = [intSeg,
        dotSeg.substring(0, n)].join('.')
    if (!hasDot) {
        formattedIntSeg = formattedIntSeg.replace(/^,/, '').replace(/\.$/, '')
    }
    return formattedIntSeg
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容