添加逗号、小数到数字输出 javascript

发布于 2024-11-30 03:04:45 字数 840 浏览 2 评论 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/

I'm using the following code to count up from a starting number. What I need is to insert commas in the appropriate places (thousands) and put a decimal point in front of the last two digits.

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")
    })
})

Executed here: http://jsfiddle.net/blackessej/TT8BH/3/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

护你周全 2024-12-07 03:04:46

查看此页面,了解有关 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!

Check out this page for explanations on slice(), split(), and substring(), as well as other String Object functions.

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!
夕嗳→ 2024-12-07 03:04:46

如果语言环境不工作,则可以使用此函数
数量=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;
}   

This function can be used for if not working locale somite
number =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;
}   
埖埖迣鎅 2024-12-07 03:04:46
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;
}
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;
}
昇り龍 2024-12-07 03:04:45
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;
}
var s = 121221;

Use the function insertDecimalPoints(s.toFixed(2));

and you get 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文