Skip to content Skip to sidebar Skip to footer

Format Number To Price

I have seen a few of these jquery things going on, and just wondered if there was a simple number formatting script. Essentially, all we wish to do, is format ( client side ) for c

Solution 1:

Setup a function like this one Javascript format currency

functionCurrencyFormatted(amount)
{
    var i = parseFloat(amount);
    if(isNaN(i)) { i = 0.00; }
    var minus = '';
    if(i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = newString(i);
    if(s.indexOf('.') < 0) { s += '.00'; }
    if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}

Then set an onchange for jQuery something like this

jQuery(document).ready(function(){

  jQuery('#price').change(function(){

      jQuery('#mydivsomewhere').val(CurrencyChange(jQuery('#price').val));
  });

});

Not sure if that is 100% correct, haven't tested it. But should call CurrencyFormat whenever the text in your input box changes. Should pass in the val of the textbox with id of price and set a div of id mydivsomewhere with the formatted value.

Post a Comment for "Format Number To Price"