All files / moneyUtil MoneyUtil.ts

100% Statements 14/14
100% Branches 3/3
100% Functions 4/4
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74                                  10x 10x                           12x 12x 12x                         17x 17x                           11x 11x 11x 11x 8x   3x   11x      
/**
 * this util module is about to provide money support utils
 * @module angle-util/moneyUtil
 */
export default {
  /**
   * 分转元
   * @author youngbeen
   * @param { integer | string } [val] - 传入的待转内容,可传入整形或者字符串
   * @return { string } 转换后的元字符串
   * @example
   * console.log(moneyUtil.fenToYuan(100))    // '1.00'
   * console.log(moneyUtil.fenToYuan(100.12))    // '1.00'
   * console.log(moneyUtil.fenToYuan('100'))    // '1.00'
   * console.log(moneyUtil.fenToYuan('100.12'))    // '1.00'
   */
  fenToYuan (val: number | string): string {
    const validVal: string = (val + '').replace(/[^\d.]/g, '')
    return (Number(validVal) / 100).toFixed(2)
  },
  /**
   * 元转分
   * @author youngbeen
   * @param { integer | string } [val] - 传入的待转内容,可传入整形或者字符串
   * @return { string } 转换后的分字符串
   * @example
   * console.log(moneyUtil.yuanToFen(1))    // '100'
   * console.log(moneyUtil.yuanToFen(1.00))    // '100'
   * console.log(moneyUtil.yuanToFen('1'))    // '100'
   * console.log(moneyUtil.yuanToFen('1.00'))    // '100'
   */
  yuanToFen (val: number | string): string {
    let validVal: number | string = (val + '').replace(/[^\d.]/g, '')
    validVal = Number(validVal)
    return Math.round(validVal * 100).toString()
  },
  /**
   * 元转万元
   * @author youngbeen
   * @param { integer | string } [val] - 传入的待转内容,可传入整形或者字符串
   * @return { string } 转换后的字符串
   * @example
   * console.log(moneyUtil.yuanToWan('100'))    // '0.01'
   * console.log(moneyUtil.yuanToWan(12345.01))    // '1.23'
   * console.log(moneyUtil.yuanToWan('12345.01', 3))    // '1.234'
   */
  yuanToWan (val: number | string, decimal: number = 2): string {
    const validVal: string = (val + '').replace(/[^\d.]/g, '')
    return (Number(validVal) / 10000).toFixed(decimal)
  },
  /**
   * 金额格式化
   * @author youngbeen
   * @param { integer | string } [val] - 传入的待转内容,可传入整形或者字符串
   * @return { string } 转换后的字符串
   * @example
   * console.log(moneyUtil.moneyStyle(1))    // '1'
   * console.log(moneyUtil.moneyStyle(1.00))    // '1.00'
   * console.log(moneyUtil.moneyStyle(1234))    // '1,234'
   * console.log(moneyUtil.moneyStyle('1234.00'))    // '1,234.00'
   */
  moneyStyle (val: number | string): string {
    let validVal: string = (val + '').replace(/[^\d.]/g, '')
    let [ prefix, suffix ] = validVal.split('.')
    prefix = prefix.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
    if (suffix) {
      suffix = '.' + suffix
    } else {
      suffix = ''
    }
    return prefix + suffix
  }
}