Js/JQuery将数字金额转换为其他国家货币格式(包括货币符号和千分符)

2023-03-06 16:24:02 浏览数 (1)

结果显示

代码语言:javascript复制
123536.8546 转换为越南盾---> 123.536,85 ₫
6536.846 转换为人民币---> ¥6,536.85

JavaScript代码

代码语言:javascript复制
// 转换为越南盾
function numToVnFormatter(s) {
    var persianDecimal = new Intl.NumberFormat("vi-VN",
        { style: "currency", currency: "VND",useGrouping: true,currencyDisplay: "symbol", maximumFractionDigits: 2 }
    );
    return  persianDecimal.format(s);
}

//转换为人民币
function numToCnFormatter(s) {
    var persianDecimal = new Intl.NumberFormat("zh-CN",
        { style: "currency", currency: "CNY",useGrouping: true,currencyDisplay: "symbol", maximumFractionDigits: 2 }
    );
    return  persianDecimal.format(s);
}

如上述js,其他国家只需要更换NumberFormat中对应的国家currency及国际化地区语言码即可.立即查看各国对照表

调用方式

代码语言:javascript复制
numToVnFormatter (123536.8546)

完整Demo

代码语言:javascript复制
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
function numToVnFormatter(s) {
    var persianDecimal = new Intl.NumberFormat("vi-VN",
        { style: "currency", currency: "VND",useGrouping: true,currencyDisplay: "symbol", maximumFractionDigits: 2 }
    );
    return  persianDecimal.format(s);
}

function numToCnFormatter(s) {
    var persianDecimal = new Intl.NumberFormat("zh-CN",
        { style: "currency", currency: "CNY",useGrouping: true,currencyDisplay: "symbol", maximumFractionDigits: 2 }
    );
    return  persianDecimal.format(s);
}

$(function(){ 
    $("#div1").text('123536.8546 转换为越南盾---> ' numToVnFormatter (123536.8546));
    $("#div2").text('6536.846  转换为人民币---> ' numToCnFormatter(6536.846));
});

    
</script>
</head>

<body>
    <div id="div1"></div>
    <div id="div2"></div>
</body>
</html>

0 人点赞