JS 编号 - 在 .fixed 后使用区域设置字符串

JS Numbers - Using locale string after .fixed

本文关键字:区域 设置 字符串 编号 fixed JS      更新时间:2023-09-26

我正在使用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+)?$/