如何将金额转换成千分位表示法?
- 内容介绍
- 文章标签
- 相关推荐
本文共计157个文字,预计阅读时间需要1分钟。
javascript/** * 转换金额为千分位 * @param {number} price - 金额 * @param {number} param - 参数 * @param {string} returns - 返回值 * @returns {string} */function toThousands(price, param, returns) { var digit=2; // 保留小数位数 price=parseFloat((price + '').replace(/[^\d.]/g, '')).toFixed(digit); var leftPart=price.split('.')[0]; return leftPart.replace(/(\d{3})(?=\d)/g, '$1,') + '.' + (price.split('.')[1] || '');}
金额转千分位/** * 金额转千分位 * @param price * @returns string */ function toThousands(price) { var digit = 2;//保留小数位数 price = parseFloat((price + "").replace(/[^\d\.-]/g, "")).toFixed(digit) + ""; var leftPart = price.split(".")[0].split("").reverse();//左侧 var rightPart = price.split(".")[1];//右侧 var thousandsResult = ""; for(var i = 0; i < leftPart.length; i ++ ) { thousandsResult += leftPart[i] + ((i + 1) % 3 == 0 && (i + 1) != leftPart.length ? "," : "");//千分标识 } return thousandsResult.split("").reverse().join("") + "." + rightPart; //拼接 }
本文共计157个文字,预计阅读时间需要1分钟。
javascript/** * 转换金额为千分位 * @param {number} price - 金额 * @param {number} param - 参数 * @param {string} returns - 返回值 * @returns {string} */function toThousands(price, param, returns) { var digit=2; // 保留小数位数 price=parseFloat((price + '').replace(/[^\d.]/g, '')).toFixed(digit); var leftPart=price.split('.')[0]; return leftPart.replace(/(\d{3})(?=\d)/g, '$1,') + '.' + (price.split('.')[1] || '');}
金额转千分位/** * 金额转千分位 * @param price * @returns string */ function toThousands(price) { var digit = 2;//保留小数位数 price = parseFloat((price + "").replace(/[^\d\.-]/g, "")).toFixed(digit) + ""; var leftPart = price.split(".")[0].split("").reverse();//左侧 var rightPart = price.split(".")[1];//右侧 var thousandsResult = ""; for(var i = 0; i < leftPart.length; i ++ ) { thousandsResult += leftPart[i] + ((i + 1) % 3 == 0 && (i + 1) != leftPart.length ? "," : "");//千分标识 } return thousandsResult.split("").reverse().join("") + "." + rightPart; //拼接 }

