zoukankan      html  css  js  c++  java
  • 数字(金钱格式)相互转化

    /**
     * 数字格式化金钱展示
     * @param {*} num 串数字
     * @returns
     */
    export const numFormat = (num) => {
      if (typeof (num) != 'number') {
        num = Number(num)
      }
      num = num.toFixed(2);
      num = parseFloat(num)
      num = num.toLocaleString();
      let floatPart = '.00' // 预定义小数部分
      let numArry = num.split('.')
      // =2表示数据有小数位
      if (numArry.length === 2) {
        floatPart = numArry[1].toString() // 拿到小数部分
        if (floatPart.length === 1) { // 补0,实际上用不着
          return numArry[0] + '.' + floatPart + '0'
        } else {
          return numArry[0] + '.' + floatPart
        }
      } else {
        return num + floatPart
      }
    }
    
    /**
     *
     * 金钱格式化数字
     * @param {*} 传字符串或者数字
     * @returns
     */
    export const number = (value) => {
      if (typeof (value) == 'number') {
        return value
      } else {
        if (value.indexOf(',') != -1) {
          return Number(value)
        } else {
          return Number(value.replace(',', ''))
        }
      }
    }
  • 相关阅读:
    异常处理
    集合面试题
    数据结构
    集合遍历
    集合汇总
    Collections工具类
    HashMap和hashTable的区别
    Map接口和Collection接口的区别
    Spark应用远程调试
    使用 maskView 设计动画
  • 原文地址:https://www.cnblogs.com/zmdComeOn/p/11946341.html
Copyright © 2011-2022 走看看