我正在使用 Money.js,一个用于转换货币的插件,并设置 div 的内容,如下所示:
currentDiv.html("<div>" + currentPrice + "</div><div class='converted'> " + rate.toFixed(0) + "</div>");
我尝试在每三位数字后用逗号分隔数字,并尝试将 .toLocaleString
添加到该行,但无法使其正常工作。
整夜都在这里寻找不同的解决方案,例如使用正则表达式等,但还没有找到任何东西......有什么想法吗?
这是全部代码:
<script src="https://raw.githubusercontent.com/openexchangerates/money.js/master/money.js"></script>
<script src="https://cdn.rawgit.com/openexchangerates/money.js/master/money.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<div class="hello">
2300
</div>
<div class="hello">
52400
</div>
<script>
$(".hello").each(function() {
var currentDiv = $(this);
var currentPrice = currentDiv.text();
var demo = function(data) {
fx.rates = data.rates
var rate = fx(currentPrice).from("GBP").to("USD");
currentDiv.html("<div>"+currentPrice +"</div><div id='converted'> " +rate.toFixed(0)+"</div>");
}
$.getJSON("http://api.fixer.io/latest", demo);
});
</script>
最佳答案
因为它是货币,所以末尾会有.##
,对吗?
/^(?!0+\.00)(?=.{1,9}(\.|$))(?!0(?!\.))\d{1,3}(,\d{3})*(\.\d+)?$/
关于javascript - JS Numbers - 在 .fixed 之后使用区域设置字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36143013/