1

i need your help by converting a american number format (1,000.00) to the european (1.000,00) in a code named Counter-Up from bfintal . (https://github.com/bfintal/Counter-Up). My convertion of the integer work, just the float number dont count up. Heres the code: Code: http://jsfiddle.net/ClaraCode/mwpvxwpp/1/

Heres the Code in raw: 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 );

Can you find the mistake of me? I spent the half day with searching... :( Thank you

Clara T.
  • 11
  • 2

2 Answers2

0

I also suggest just swapping the . and ,
To handle the float just make it a string first:

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

console.log(tmpInput);
Plato
  • 10,812
  • 2
  • 41
  • 61
  • Thanks but the floatinput is in the format with a comma: e.g. 1,01 and the end format is in the european float format too , that means 1,01. The problem lay in the JS-Code that awaits the american format (e.g. 1.01). Just like in my jsfiddle. I want that the code can handle the european float input and count it up from zero to the wanted number – Clara T. Jun 13 '15 at 09:38
  • so, run `String(input).test(/,/)` and if it's true, handle the european format, if false, handle american format – Plato Jun 13 '15 at 13:37
0

I solved it

            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
                }
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219