Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/mootools/1.2.4/mootools-yui-compressed.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
</style>
</head>
<body>
  <h1 id="total">$0.99</h1>
  <button id="high">Change to $999.99</button>
  <button id="med">Change to $499.02</button>
  <button id="low">Change to $0.00</button>
  <script type="text/javascript" charset="utf-8">
    //<![CDATA[
      window.addEvent('domready', function(){
        var total = $('total');
        var totalFx = new Fx.CashRegister(total, {duration: 2000});
        
        $('high').addEvent('click', function(){
          totalFx.start(999.99);
        });
        
        $('med').addEvent('click', function(){
          totalFx.start(499.02);
        });
        
        $('low').addEvent('click', function(){
          totalFx.start(0.02);
        });
      });
    //]]>
    </script>
</body>
</html>
 
Fx.CashRegister = new Class({
  Extends: Fx,
  
  options: {
    precision: 2,
    prefix: '$',
    thousandsSeparator: ',',
    decimalSeparator: '.'
    },
  
  initialize: function(element, options){
    this.element = this.subject = document.id(element);
    this.parent(options);
  },
  
  start: function(to){
    if (!this.check(to)) return this;
    
    var current_value = this.element.get('text');
    current_value = current_value.substring(this.options.prefix.length);
    current_value = this.stripSeparators(current_value);
    current_value = current_value.toFloat().round(this.options.precision);
    
    this.from = current_value;
    this.to = to;
    this.time = 0;
        this.transition = this.getTransition();
        this.startTimer();
        this.onStart();
        return this;
  },
  
  set: function(now){
    var new_value = this.options.prefix + now.round(this.options.precision);
    
    this.element.set('text', this.addSeparators(new_value));
  },
  
  addSeparators: function(number)
  {
    var parts = number.toString().split(this.options.decimalSeparator);
    
    var dollars = parts[0];
    if (parts[1]) { var cents = parts[1]; }
    
    var rgx = /(\d+)(\d{3})/;
    
    while (rgx.test(dollars)) {
        dollars = dollars.replace(rgx, '$1' + this.options.thousandsSeparator + '$2');
    }
    
    var output = dollars;
    
    if (this.options.precision > 0) {
      if (cents) {
        output += this.options.decimalSeparator + cents;
      } else {
        output += '0' * this.options.precision;
      }
    }
    
    return output;
  },
  
  stripSeparators: function(string) {
    var result = string.replace(this.options.decimalSeparator, '.');
    var pattern = new RegExp(this.options.thousandsSeparator, 'g');
    var matches = result.match(pattern)
    
    if (matches) {
      matches.each(function() {
        result = string.replace(pattern, '');
      });
    }
    
    return result;
  }
});
Output

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers