javascript - format number to price - Stack Overflow

I have seen a few of these jquery things going on, and just wondered if there was a simple number forma

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745353265a4623965.html

相关推荐

  • javascript - format number to price - Stack Overflow

    I have seen a few of these jquery things going on, and just wondered if there was a simple number forma

    7小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信