目的地-Destination
文章

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

烂笔头 · 2 条评论 · 2,600 浏览 · 4 点赞

## 结果显示
123536.8546 转换为越南盾---> 123.536,85 ₫
6536.846 转换为人民币---> ¥6,536.85

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及国际化地区语言码即可.立即查看各国对照表

调用方式

numToVnFormatter (123536.8546)

完整Demo

<!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>
CONVERSATION

2 comments

Write a Comment

  1. 海角七号江苏省 苏州市 电信 · iPhone
    回复
    可以可以,正是我想要的!!!" rel="nofollow ugc noopener" target="_blank">https://www.80srz.com/usr/themes/cactus/lib/OwO/alu/jingxi.png">
    1. Chrison江苏省 南京市 电信 · iPhone
      回复
      回复 @海角七号" rel="nofollow ugc noopener" target="_blank">http://blog.chrison.cn/usr/themes/cactus/lib/OwO/alu/xianhua.png">