I am using money.js, a plugin to convert currency and am setting the content of a div like so:
currentDiv.html("<div>" + currentPrice + "</div><div class='converted'> " + rate.toFixed(0) + "</div>");
I am trying to separate the number with commas after every three digits and have tried adding .toLocaleString
to the line but couldn't get it to work.
Have been looking on here all night for different solutions such as with regex etc. but haven't found anything yet...any ideas?
This is all the code:
<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>