LoginSignup
0
0

More than 5 years have passed since last update.

javascriptで金額を3桁区切りにする

Posted at

ECサイトとかで金額を3桁ずつカンマで区切りたいというときがあります。
だけど、DBでは数値で持っているので、3桁ずつ区切るのはjsでやらなくてはいけない。
そんな時はこの関数を使ってみてください。

function addFigure(str) {
    var num = new String(str).replace(/,/g, "");
    while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
    return num;
}

そして価格のところを全て同じclassにしてjQueryのeachで回せば、OKです。

$('.product_price').each(function(){
    $(this).html(addFigure($(this).html()));
});
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0