将美国数字格式转换为欧洲

时间:2015-06-12 19:25:29

标签: javascript

我需要你的帮助,将美国数字格式(1,000.00)转换为欧洲(1.000,00),代码为来自bfintal的Counter-Up代码。 (https://github.com/bfintal/Counter-Up)。我转换整数的工作,只是浮点数不算数。 下面是代码: 代码:http://jsfiddle.net/ClaraCode/mwpvxwpp/1/

原始代码: HTML

<span class="count">12.000</span><br>
<span class="count">1,20</span><br>
<span class="count">1.000</span>

JS:

(function( $ ){
  "use strict";

  $.fn.counterUp = function( options ) {

    // Defaults
    var settings = $.extend({
        'time': 400,
    'delay': 10
}, options);

return this.each(function(){

    // Store the object
    var $this = $(this);
    var $settings = settings;

    var counterUpper = function() {
        var nums = [];
        var divisions = $settings.time / $settings.delay;
        var num = $this.text();
        var isComma = /[0-9]+.[0-9]+/.test(num);
        num = num.replace(/\./g, '');
        var isInt = /^[0-9]+$/.test(num);
        var isFloat = /^[0-9]+,[0-9]+$/.test(num);
        var decimalPlaces = isFloat ? (num.split(',')[1] || []).length : 0;

        // Generate list of incremental numbers to display
        for (var i = divisions; i >= 1; i--) {

            // Preserve as int if input was int
            var newNum = parseInt(num / divisions * i);

            // Preserve float if input was float
            if (isFloat) {
                newNum = parseFloat(num / divisions * i).toFixed(decimalPlaces);
            }

            // Preserve commas if input had commas
            if (isComma) {
                while (/(\d+)(\d{3})/.test(newNum.toString())) {
                    newNum = newNum.toString().replace(/(\d+)(\d{3})/, '$1'+'.'+'$2');
                }
            }

            nums.unshift(newNum);
        }

        $this.data('counterup-nums', nums);
        $this.text('0');

        // Updates the number until we're done
        var f = function() {
            $this.text($this.data('counterup-nums').shift());
            if ($this.data('counterup-nums').length) {
                setTimeout($this.data('counterup-func'), $settings.delay);
            } else {
                delete $this.data('counterup-nums');
                $this.data('counterup-nums', null);
                $this.data('counterup-func', null);
            }
        };
        $this.data('counterup-func', f);

        // Start the count up
        setTimeout($this.data('counterup-func'), $settings.delay);
    };

    // Perform counts when the element gets into view
    $this.waypoint(counterUpper, { offset: '100%', triggerOnce: true });
});

  };

})( jQuery );
你能找到我的错吗?我花了半天时间搜索...... :( 谢谢

2 个答案:

答案 0 :(得分:0)

我还建议换掉。并且,
要处理浮动,只需先将其设为字符串:

var floatInput = 1.01;
var tmpInput = String(floatInput);
tmpInput = tmpInput.replace(/,/, '♣')
  .replace(/\./, ',')
  .replace(/♣/, '.');

console.log(tmpInput);

答案 1 :(得分:0)

嗨,我有同样的问题,我解决了它

&#13;
&#13;
            var counterUpper = function() {

                var nums = [];
                var divisions = $settings.time / $settings.delay;
                var num = $this.text();
                var isComma = /[0-9]+,[0-9]+/.test(num);
                num = num.replace(/,/g, '.'); //change this line
                var isInt = /^[0-9]+$/.test(num);
                var isFloat = /^[0-9]+\.[0-9]+$/.test(num);
                var decimalPlaces = isFloat ? (num.split('.')[1] || []).length : 0;

                var setCharAt = function(str, index, chr) {
                  debugger;
                  if (index > str.length - 1) return str;
                  return str.substr(0, index) + chr + str.substr(index + 1);
                }

                // Generate list of incremental numbers to display
                for (var i = divisions; i >= 1; i--) {

                  // Preserve as int if input was int
                  var newNum = parseInt(num / divisions * i);

                  // Preserve float if input was float
                  if (isFloat) {
                    newNum = parseFloat(num / divisions * i).toFixed(decimalPlaces);
                  }

                  // Preserve commas if input had commas
                  if (isComma) {
                    debugger;
                    while (/(\d+)(\d{3})/.test(newNum.toString())) {
                      newNum = newNum.toString().replace(/(\d+)(\d{3})/, '$1' + ',' + '$2');

                    }
                    newNum = newNum.replace('.', ',');
                  }

                  nums.unshift(newNum); //add this line to your code
                }
&#13;
&#13;
&#13;