<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
/*
以货币形式显示
currencyShow(12345) => "¥12,345.00"
*/
function currencyShow (floatvar) {
var fx = parseFloat(floatvar);
if (isNaN(fx)) {
return floatvar;
};
var fx = Math.round(fx * 100) / 100;
var sx = fx.toString();
var pos_decimal = sx.indexOf('.');
if (pos_decimal < 0) {
pos_decimal = sx.length;
sx += '.';
};
while (sx.length <= pos_decimal + 2) {
sx += '0';
};
var iLen = sx.indexOf('.');
if (iLen <= 3) {
return "¥" + sx;
};
var arr = new Array();
for (var i = 0; i < iLen; i++) {
arr[i] = sx.charAt(i);
};
var rstx = "";
for (var i = 0; i < iLen % 3; i++) {
rstx += sx.charAt(i);
};
for (var i = iLen % 3; i < iLen; i++) {
if (i % 3 == iLen % 3 && i < (iLen - 1) && i > 0) {
rstx += ",";
};
rstx += sx.charAt(i);
};
rstx += sx.substring(sx.length - 3);
rstx = "¥" + rstx;
return rstx;
}
</script>
</body>
</html>
前端js实现货币转换
最新推荐文章于 2024-07-11 20:37:29 发布