0

我正在使用以下代码从起始数字开始计数。我需要的是在适当的位置(千位)插入逗号,并在最后两位数前面放一个小数点。

function createCounter(elementId,start,end,totalTime,callback)
{
    var jTarget=jQuery("#"+elementId);
    var interval=totalTime/(end-start);
    var intervalId;
    var current=start;
    var f=function(){
        jTarget.text(current);
        if(current==end)
        {
            clearInterval(intervalId);
            if(callback)
            {
                callback();
            }
        }
        ++current;
    }
    intervalId=setInterval(f,interval);
    f();
}
jQuery(document).ready(function(){
    createCounter("counter",12714086+'',9999999999,10000000000000,function(){
        alert("finished")
    })
})

在这里执行:http: //jsfiddle.net/blackessej/TT8BH/3/

4

4 回答 4

1
var s = 121221;

使用功能insertDecimalPoints(s.toFixed(2));

你得到1,212.21

function insertDecimalPoints(s) {
    var l = s.length;
    var res = ""+s[0];
    console.log(res);
    for (var i=1;i<l-1;i++)
    {
        if ((l-i)%3==0)
            res+= ",";
        res+=s[i];
    }
    res+=s[l-1];

    res = res.replace(',.','.');

    return res;
}
于 2012-08-26T05:52:03.810 回答
0

此函数可用于如果不工作的语言环境体节
数 =1000.234;number=insertDecimalPoints(number.toFixed(3));

   function insertDecimalPoints(s) {
   console.log(s);
    var temaparray = s.split(".");
    s = temaparray[0];
    var l = s.length;
    var res = ""//+s[0];
    console.log(res);
    for (var i=0;i<l-1;i++)
    {
        if ((l-i)%3==0 && l>3)
            res+= ",";
        res+=s[i];
    }
    res+=s[l-1];
    res =res +"."+temaparray[1];
    return res;
}   
于 2014-02-04T11:39:06.103 回答
0

查看此页面以获取有关 slice()、split() 和 substring() 以及其他字符串对象函数的说明。

var num = 3874923.12 + ''; //converts to a string
numArray = num.split('.'); //numArray[0] = 3874923 | numArray[1] = 12;

commaNumber = '';
i = numArray[0].length;
do
{
    //we don't want to start slicing from a negative number. The following line sets sliceStart to 0 if i < 0. Otherwise, sliceStart = i
    sliceStart = (i-3 >= 0) ? i-3 : 0;

    //we're slicing from the right side of numArray[0] because i = the length of the numArray[0] string.
    var setOf3 = numArray[0].slice(sliceStart, i);

    commaNumber = setOf3 + ',' + commaNumber; //prepend the new setOf3 in front, along with that comma you want

    i -= 3; //decrement i by 3 so that the next iteration of the loop slices the next set of 3 numbers
}
while(i >= 0)

//result at this point: 3,874,923,

//remove the trailing comma
commaNumber = commaNumber.substring(0,commaNumber.length-1);

//add the decimal to the end
commaNumber += '.' + numArray[1];

//voila!
于 2011-08-17T18:08:50.880 回答
0
function convertDollar(number) {
    var num =parseFloat(number);
    var n = num.toFixed(2);
    var q =Math.floor(num);
    var z=parseFloat((num).toFixed(2)).toLocaleString();
    var p=(parseFloat(n)-parseFloat(q)).toFixed(2).toString().replace("0.", ".");
    return z+p;
}
于 2017-03-15T21:04:25.437 回答