2016-03-21 5 views
1

sonra .fixed yerel dize kullanarak:JS Numaraları - Ben para birimi dönüştürmek için, bir eklenti money.js kullanıyorum ve şöyle bir div içeriğini kuruyorum

currentDiv.html("<div>" + currentPrice + "</div><div class='converted'> " + rate.toFixed(0) + "</div>"); 

Birlikte numarayı ayırmak için çalışıyorum her üç basamaktan sonra virgüllere ve .toLocaleString satırına eklemeyi denediniz ancak işe yaramayacaksınız.

Bütün gece burada regex vb. Farklı çözümler bulmaya çalışıyordum ama henüz bir şey bulamadınız ... herhangi bir fikir?

Bu, tüm kod şudur: para olduğu için

<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> 
+1

http://stackoverflow.com/questions/3883342/add-commas-to-a-number-in-jquery –

+0

bu deneyin: parseFloat (oranı) .toFixed (0) .Kapağı (/ (\ d) (? = (\ d {3}) + \.)/g, "$ 1,") – DinoMyte

+0

@DinoMyte Korkarım ki hiçbir şey yapmıyor ... – dwinnbrown

cevap

0

, sağ bunun sonunda .## olacak?

/^(?!0+\.00)(?=.{1,9}(\.|$))(?!0(?!\.))\d{1,3}(,\d{3})*(\.\d+)?$/ 
İlgili konular