最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - format number to price - Stack Overflow

programmeradmin2浏览0评论

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 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

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 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

Share Improve this question asked Nov 14, 2010 at 1:39 422422 5,77024 gold badges86 silver badges140 bronze badges 3
  • 1 Check this question in stackoverflow itself stackoverflow./questions/149055/… – kobe Commented Nov 14, 2010 at 1:45
  • looked at that, but seems very heavy. Also no actual definitive answer that would solve our requirement, unless I am looking in the wrong place – 422 Commented Nov 14, 2010 at 2:01
  • How could I incorporate this: plugins.jquery./files/jquery.number_format.js_0.txt into actual html page ? and apply to the form field – 422 Commented Nov 14, 2010 at 2:06
Add a ment  | 

1 Answer 1

Reset to default 3

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)

  1. 暂无评论