【问题标题】:JQuery/JavaScript increment numberJQuery/JavaScript 增量编号
【发布时间】:2009-07-30 14:33:03
【问题描述】:

我正在尝试以给定值每秒递增一个数字并使用 JavaScript 或 JQuery 保留格式

我正在努力做到这一点。

假设我有一个这样的数字:

1412015

每秒可以递增的数字是可变的,可以是 0.1 和 2 之间的任何值。

是否有可能,如果它必须以每秒递增的值是 0.54 来增加数字并具有以下输出:

1,412,016
1,412,017
1,412,018

谢谢

伊夫

【问题讨论】:

    标签: javascript jquery numbers increment


    【解决方案1】:

    我不太确定我是否理解您的增量案例以及您想要展示的内容。 不过,我决定提出一个格式化数字的解决方案。

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

    我已经尝试用 cmets 来描述这些线条,以防你好奇

    数组解析版本:

    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

    【讨论】:

      【解决方案2】:

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

      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;
         }
      }
      

      【讨论】:

      • 谢谢,我不会做那么多javascript,是否也可以像1,412,016这样格式化数字?
      • 您的代码只适用于大于 6 的数字。如果他想要 1,548 怎么办?
      【解决方案3】:
      function rounded_inc(x, n) {
        return x + Math.ceil(n);
      }
      
      var x = 1412015;
      x = rounded_inc(x, 0.54);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-22
        • 1970-01-01
        • 1970-01-01
        • 2012-06-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多