繁体   English   中英

jQuery / JavaScript增量编号

[英]JQuery/JavaScript increment number

我试图每秒增加一个给定值的数字,并使用JavaScript或JQuery保留格式

我正在努力做到这一点。

说我有一个像这样的数字:

1412015

可以每秒增加的数字是可变的,可以在0.1到2之间。

如果必须每秒增加一次的值是0.54以增加该数字并具有以下输出,是否可以:

1412016
1412017
1412018

谢谢

EEF

我不太确定我了解您的递增情况以及您要显示的内容。 但是,我决定采用一种格式化数字的解决方案。

我有两个版本的数字格式例程,一个版本解析一个数组,另一个版本使用正则表达式进行格式化。 我承认它们不是最容易阅读的,但是我很高兴想到这种方法。

我试图用注释来描述行,以防您好奇

数组解析版本:

function formatNum(num) {
    //Convert a formatted number to a normal number and split off any 
    //decimal places if they exist
    var parts = String( num ).replace(/[^\d.]-/g,'').split('.');
    //turn the string into a character array and reverse
    var arr = parts[0].split('').reverse();

    //initialize the return value
    var str = '';

    //As long as the array still has data to process (arr.length is 
    //anything but 0)
    //Use a for loop so that it keeps count of the characters for me
    for( var i = 0; arr.length; i++ ) {
        //every 4th character that isn't a minus sign add a comma before 
        //we add the character
        if( i && i%3 == 0 && arr[0] != '-' ) {
            str  = ',' + str ;
        }

        //add the character to the result
        str  = arr.shift() + str ;
    }

    //return the final result appending the previously split decimal place 
    //if necessary
    return str + ( parts[1] ? '.'+parts[1] : '' );
}

正则表达式版本:

function formatNum(num) {
    //Turn a formatted number into a normal number and separate the 
    //decimal places
    var parts = String( num ).replace(/[^\d.]-/g,'').split('.');
    //reverse the string
    var str = parts[0].split('').reverse().join('');
    //initialize the return value
    var retVal = '';

    //This gets complicated. As long as the previous result of the regular 
    //expression replace is NOT the same as the current replacement, 
    //keep replacing and adding commas.
    while( retVal != (str = str.replace(/(\d{3})(\d{1,3})/,'$1,$2')) ) {
        retVal = str;
    }

    //If there were decimal points return them back with the reversed string
    if( parts[1] ) {
        return retVal.split('').reverse().join('') + '.' + parts[1];
    }

    //return the reversed string
    return retVal.split('').reverse().join('');
}

假设您想以每秒0.54的增量输出格式化的数字,则可以使用一个间隔来进行增量和输出。

仅带Firebug的超短Firefox示例:

var num = 1412015;

setInterval(function(){
    //Your 0.54 value... why? I don't know... but I'll run with it.
    num += 0.54;
    console.log( formatNum( num ) );
},1000);

您可以在此处查看全部操作: http : //jsbin.com/opoze

要每秒增加一个值,请使用以下结构:

var number = 0; // put your initial value here

function incrementNumber () {
    number += 1; // you can increment by anything you like here
}

// this will run incrementNumber() every second (interval is in ms)
setInterval(incrementNumber, 1000); 

这将为您格式化数字:

function formatNumber(num) {
   num = String(num);

   if (num.length <= 3) {
      return num;
   } else {
      var last3nums = num.substring(num.length - 3, num.length);
      var remindingPart = num.substring(0, num.length - 3);
      return formatNumber(remindingPart) + ',' + last3nums;
   }
}
function rounded_inc(x, n) {
  return x + Math.ceil(n);
}

var x = 1412015;
x = rounded_inc(x, 0.54);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM