js中常见的金额转换方式

1.js转换金钱为中文单位元、万元、亿元、万亿

javascript 复制代码
function unitConvert(num) {
   var moneyUnits = ["元", "万元", "亿元", "万亿"]
   var dividend = 10000;
   var curentNum = num;
   //转换数字
   var curentUnit = moneyUnits[0];
   //转换单位
    for (var i = 0; i <4; i++) {
           curentUnit = moneyUnits[i]
           if(strNumSize(curentNum)<5){
           break;
       }
       curentNum = curentNum / dividend
}
var m = {num: 0, unit: ""}
      m.num = curentNum.toFixed(2)
      m.unit = curentUnit;
      return m;
     }
 
function strNumSize(tempNum){
      var stringNum = tempNum.toString()
      var index = stringNum.indexOf(".")
      var newNum = stringNum;
      if(index!=-1){
          newNum = stringNum.substring(0,index)
       }
       return newNum.length
}
 
//调用并且得到返回值
var data=unitConvert(100000);
console.log(data.num+data.unit) //10.00万元

2. 金额格式化处理

javascript 复制代码
 // 金额 格式化
function  outputmoney(value) {  
      if (!value && value !== 0) return '-';
      var intPart = Number(value) | 0; //获取整数部分
      var intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,'); //将整数部分逢三一断
  
      var floatPart = ".00"; //预定义小数部分
      var value2Array = value.toString().split(".");
  
      //=2表示数据有小数位
      if (value2Array.length == 2) {
          floatPart = value2Array[1].toString(); //拿到小数部分
  
          if (floatPart.length == 1) { //补0,实际上用不着
              return intPartFormat + "." + floatPart + '0';
          } else {
              return intPartFormat + "." + floatPart;
          }
      } else {
          return intPartFormat + floatPart;
      }
    }
console.log(outputmoney(1111)); //1,111.00
console.log(outputmoney(11.56812111111)); //11.56812111111

// style 格式化时使用的样式
// "decimal"表示纯数字格式 , "currency"表示货币格式, 和"percent"表示百分比格式; 默认值是 "decimal"

// currency  在货币格式化中使用的货币符号
// "USD" 表示美元, "EUR" 表示欧元, "CNY"是人民币
// 没有默认值,如果样式是"currency",必须提供货币属性. minimumFractionDigits
// 使用的小数位数的最小数目.可能的值是从0到20;默认为普通的数字和百分比格式为0;
// 格式化金额(后面两位小数会四舍五入)
function  moneyFormats(value){
		let a=Number(value);  //转为数字格式
		
		let b=a.toLocaleString('zh', { style: 'currency', currency: 'CNY' });
		
		return b;
	}

console.log(moneyFormats(23.547))    //¥23.55
javascript 复制代码
function formatMoney(s, type) {  
    if (/[^0-9\.]/.test(s))  
        return "0";  
    if (s == null || s == "")  
        return "0";  
    s = s.toString().replace(/^(\d*)$/, "$1.");  
    s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");  
    s = s.replace(".", ",");  
    var re = /(\d)(\d{3},)/;  
    while (re.test(s))  
        s = s.replace(re, "$1,$2");  
    s = s.replace(/,(\d\d)$/, ".$1");  
    if (type == 0) {// 不带小数位(默认是有小数位)
        var a = s.split(".");  
        if (a[1] == "00") {  
            s = a[0];  
        }  
    }  
    return s;  
}  
console.log(formatMoney('11110.335',3)); // 11,110.33
相关推荐
小白学习日记41 分钟前
【复习】HTML常用标签<table>
前端·html
john_hjy44 分钟前
11. 异步编程
运维·服务器·javascript
风清扬_jd1 小时前
Chromium 中JavaScript Fetch API接口c++代码实现(二)
javascript·c++·chrome
丁总学Java1 小时前
微信小程序-npm支持-如何使用npm包
前端·微信小程序·npm·node.js
yanlele1 小时前
前瞻 - 盘点 ES2025 已经定稿的语法规范
前端·javascript·代码规范
It'sMyGo2 小时前
Javascript数组研究09_Array.prototype[Symbol.unscopables]
开发语言·javascript·原型模式
懒羊羊大王呀2 小时前
CSS——属性值计算
前端·css
xgq2 小时前
使用File System Access API 直接读写本地文件
前端·javascript·面试
李是啥也不会2 小时前
数组的概念
javascript
用户3157476081352 小时前
前端之路-了解原型和原型链
前端