在本例中,如何在Javascript中添加分隔数千的空格

时间:2014-04-18 07:39:29

标签: javascript jquery

我这里有两个功能。一个添加“,”用于分隔数千个,如1234 - > 1 234.还有一个增加功能。

增加的功能只是打印123456,我想结合这些,我虽然可以改变:

$ this.html(++电流);

于: $ this.html(addSpaces(++电流));

但它不起作用。请帮帮我,我该如何解决这个问题?

function addSpaces(nStr)
{
    nStr += "";
    x = nStr.split(".");
    x1 = x[0];
    x2 = x.length > 1 ? "." + x[1] : "";
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, "$1" + " " + "$2");
    }
    return x1 + x2;
}   


function count($this) {

    var current = parseInt($this.html(), 10);
    current = current + 13 /* This is increment */

    $this.html(++current);
    if (current > $this.data("count")) {
        $this.html($this.data("count"));
    } else {
        setTimeout(function() { count($this); }, 100);
    }

}

3 个答案:

答案 0 :(得分:3)

更新我修改了你的jsfiddle

由于current将从您的格式化值中反复解析,我们需要从中删除空格

current = parseInt(($this.html()).split(' ').join(''), 10)

此外,您需要在名为current

的变量下跟踪增量nextString的字符串值的跟踪

您希望您的号码最多按3位数分组。问题是,如果3不分割你的字符串的长度,你可能有一个remainder。隔离字符串的remainder部分(最左侧)后,您可以将所有其他字符串分组3。

DEMO

function addSpaces(nStr)
{
    var remainder = nStr.length % 3;
    return (nStr.substr(0, remainder) + nStr.substr(remainder).replace(/(\d{3})/g, ' $1')).trim();
}

function count($this) {

    var current = parseInt(($this.html()).split(' ').join(''), 10),
        nextString = (current+13) + '';

    $this.html(addSpaces(nextString));

    if (current > $this.data("count")) {
        $this.html($this.data("count"));
    } else {
        setTimeout(function() {
            count($this);

        }, 100);
    }

}

答案 1 :(得分:1)

或者你可以使用像toLocaleString()这样的东西,如果你想要的话:

var number = 3500;

console.log(number.toLocaleString()); // Displays "3,500" if in U.S. English locale

var number = 123456.789;

// German uses comma as decimal separator and period for thousands
alert(number.toLocaleString("de-DE"));
// → 123.456,789

// Arabic in most Arabic speaking countries uses real Arabic digits
alert(number.toLocaleString("ar-EG"));
// → ١٢٣٤٥٦٫٧٨٩

// India uses thousands/lakh/crore separators
alert(number.toLocaleString("en-IN"));
// → 1,23,456.789

// the nu extension key requests a numbering system, e.g. Chinese decimal
alert(number.toLocaleString("zh-Hans-CN-u-nu-hanidec"));
// → 一二三,四五六.七八九

// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
alert(number.toLocaleString(["ban", "id"]));
// → 123.456,789

请参阅:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

答案 2 :(得分:0)

第一个可以使用,你可以使用以下第二个;

<div class="count">1234</div>

和js;

$(".count").on("click", function() {
        $this = $(this);
        var current = parseInt($this.html(), 10);
        current = current + 13 /* This is increment */
        $this.html(++current);
        if (current > $this.data("count")) {
            $this.html($this.data("count"));
        } else {
            setTimeout(function() { count($this); }, 100);
        }    
    });

以下是工作演示: Demo (点击演示中的第一个div)