开发者

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 checking purposes only, the number entered in a field. To show somewhere else on开发者_StackOverflow the page ( presumably in a div ) the formatted price they entered.

So lets say, field

input id="price" name="price" size="50" type="text" class="medium" /

And they enter 1234560

I want to show somewhere else on the page, : You Entered : $1,234,560.00 as the price. Is this correct ?

This is only for visual purposes only. Alternatively, changing the value of what they type and formatting it "live" could be an option, however the value we want to send to the db is pure numerics, ie: 1234560


Setup a function like this one Javascript format currency

function CurrencyFormatted(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 = new String(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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜