Variable numberUtilsConst

numberUtils: {
    accAdd(arg1, arg2): string;
    accDiv(arg1, arg2, retainNum?): string;
    accMul(arg1, arg2): number;
    accSub(arg1, arg2): string;
    addDot(num): string;
    to100Num(rate): number;
    to100Rate(num, fiexd?): string | number;
    tokw(num, isAddUnit?): string | number | {
        num: string | number;
        unit: string;
    };
} = ...

数字相关工具方法

Type declaration

  • accAdd:function
    • 加法运算

      Parameters

      • arg1: string | number

        被加数

      • arg2: string | number

        加数

      Returns string

      加法运算的结果

      Example

      numberUtils.accAdd(1, 2) // '3.00'
      numberUtils.accAdd(1.000000, 2.000000) // '3.00'
      numberUtils.accAdd(1.001, 2.01) // '3.011'
  • accDiv:function
    • 除法运算

      Parameters

      • arg1: string | number

        被除数

      • arg2: string | number

        除数

      • retainNum: number = 3

        保留小数点后的位数, 默认3

      Returns string

      Example

      numberUtils.accDiv(1, 2) // '0.500'
      numberUtils.accDiv(1.001, 2.01) // '0.498'
      numberUtils.accDiv(1.05, 50) // '0.021'
  • accMul:function
    • 乘法运算

      Parameters

      • arg1: string | number

        被乘数

      • arg2: string | number

        乘数

      Returns number

      乘积结果

      Example

      numberUtils.accMul(1, 2) // 2
      numberUtils.accMul(1.001, 2.01) // 2.01201
      numberUtils.accMul(2.00, 1.00) // 2
      numberUtils.accMul(2.00, 1.001) // 2.002
  • accSub:function
    • 减法运算

      Parameters

      • arg1: string | number

        被减数

      • arg2: string | number

        减数

      Returns string

      减法运算结果

      Example

      numberUtils.accSub(1, 2) // '-1.00'
      numberUtils.accSub(1.000000, 2.000000) // '-1.00'
      numberUtils.accSub(1.001, 2.01) // '-1.009'
  • addDot:function
    • 为数字添加小数点,并保留2位数,如果已经有小数点则不处理

      Parameters

      • num: string | number

        需要转换的数字。

      Returns string

      转换后的数字。

      Example

      numberUtils.addDot(0) // '0.00'
      numberUtils.addDot(0.001) // '0.001'
      numberUtils.addDot(1000) // '1000.00'
      numberUtils.addDot(1000.001) // '1000.001'
      numberUtils.addDot(1.000000) // '1.00'
      numberUtils.addDot(1.000100) // '1.0001'
      numberUtils.addDot('1') // '1.00'
      numberUtils.addDot('1.000000') // '1.000000'
  • to100Num:function
    • 百分比转换为数字

      Parameters

      • rate: string | number

        要转换为数字的百分比值。

      Returns number

      返回转换后的数字值,如果输入无效或小于等于 0,则返回 0。

      Example

      numberUtils.to100Num(20.2) // 2020
      numberUtils.to100Num(0.2) // 20
      numberUtils.to100Num(0.02) // 2
      numberUtils.to100Num(-2) // 0
  • to100Rate:function
    • 数字转换为百分比

      Parameters

      • num: string | number

        要转换为百分比的数字。

      • fiexd: number = 4

        保留的小数位数,默认为 4。

      Returns string | number

      返回转换后的百分比值,如果输入无效或小于等于 0,则返回 0。

      Example

      numberUtils.to100Rate(20) // '0.2000'
      numberUtils.to100Rate(0.2) // '0.0020'
      numberUtils.to100Rate(0.02) // '0.0002'
      numberUtils.to100Rate(-2) // 0
      numberUtils.to100Rate(20, 2) // '0.20'
  • tokw:function
    • 将数字转化为带有单位的字符串或对象。如果数字大于或等于 10000,单位为 'w';如果数字大于或等于 1000,单位为 'k'。

      Parameters

      • num: string | number

        需要转化的数字。

      • isAddUnit: boolean = true

        true,返回带有单位的字符串;false,则返回一个包含数字和单位的对象。

      Returns string | number | {
          num: string | number;
          unit: string;
      }

      转换后带单位的数字, 或者包含数字和单位的对象。

      Example

      numberUtils.tokw('') // 0
      numberUtils.tokw('1') // '1'
      numberUtils.tokw('1000') // '1k'
      numberUtils.tokw('1000000') // '100w'
      numberUtils.tokw(1000000, false) // { num: 100, unit: "w" }
      numberUtils.tokw(1000, false) // { num: 1, unit: "k" }

Generated using TypeDoc