-1

10 進数値をフォーマットするタスクがあります。テキスト ボックスがあります。ユーザーはさまざまな値を入力できます。これを動的に行うにはどうすればよいですか?

function ReplaceNumberWithCommas(yourNumber) {
    //Seperates the components of the number
    var components = yourNumber.toString().split(".");
    //Comma-fies the first part
    components [0] = components [0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

デモ

4

2 に答える 2

0

正規表現の 3 を 2 に置き換えるだけです...

$('#number').focusout(function() {
    var a = $('#number').val();
    //Seperates the components of the number
    var components = a.toString().split(".");
    //Comma-fies the first part
    components [0] = components [0].replace(/\B(?=(\d{2})+(?!\d))/g, ","); // here in the regex put 2
});
于 2013-06-27T12:04:09.730 に答える