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

javascript - text box decimal format fixing - Stack Overflow

programmeradmin1浏览0评论

I have a simple text box and I am entering number value to this.And i want to format the number value to two decimal places.Like If i enter the 10 in the text box and after pressing entering or leaving textbox it should be converted into 10.00.Please tell me if there exist any possibility to do this using javascript or vb or asp property of textbox.I already tried in JavaScript on enter key or tab key press but it convert it into 10.00 each time when i press the enter or tab.here is code

//Make Formatting with .00


document.onkeydown = function disableKeys() {

    if( typeof event != 'undefined' ) {
      if( (event.keyCode == 9) ||
          (event.keyCode == 13) ) {
          var a;
          var b;
          a=parseFloat(document.getElementById('txtpkrusd').value,10);
          b=parseFloat(document.getElementById('txtRatelb').value,10);
          document.getElementById('txtpkrusd').value=formatNumber(a);         
          document.getElementById('txtRatelb').value=formatNumber(b);         

      }
    };
};

I have a simple text box and I am entering number value to this.And i want to format the number value to two decimal places.Like If i enter the 10 in the text box and after pressing entering or leaving textbox it should be converted into 10.00.Please tell me if there exist any possibility to do this using javascript or vb or asp property of textbox.I already tried in JavaScript on enter key or tab key press but it convert it into 10.00 each time when i press the enter or tab.here is code

//Make Formatting with .00


document.onkeydown = function disableKeys() {

    if( typeof event != 'undefined' ) {
      if( (event.keyCode == 9) ||
          (event.keyCode == 13) ) {
          var a;
          var b;
          a=parseFloat(document.getElementById('txtpkrusd').value,10);
          b=parseFloat(document.getElementById('txtRatelb').value,10);
          document.getElementById('txtpkrusd').value=formatNumber(a);         
          document.getElementById('txtRatelb').value=formatNumber(b);         

      }
    };
};
Share Improve this question edited Mar 20, 2012 at 9:45 Adeel Aslam asked Mar 20, 2012 at 6:55 Adeel AslamAdeel Aslam 1,29410 gold badges38 silver badges72 bronze badges 1
  • To format the number you just need .tofixed like this [code] var num = 2.4; alert(num.toFixed(2)); // 2.40 ; hope this helps cheers – Tats_innit Commented Mar 20, 2012 at 7:01
Add a ment  | 

2 Answers 2

Reset to default 2
var num = 10;

var result = num.toFixed(2);  //will give 10.00

Examples

hiya hope this helps: (apart from my ment above i.e. to round use .toFixed(num))

demo (enter the number and press enter) : http://jsfiddle/6wG26/5/ OR Enter and Tab keys both here http://jsfiddle/Mtw7c/7/

Please; let me if this is not what you want I will delete my post; but this will help you.

Since you dint had much code up I have made forked sample here:

**HTML:**

<input type="text" name="one" class="currency">


**JQuery code:**

    (function($) {
        $.fn.currencyFormat = function() {
            this.each( function( i ) {
                $(this).change( function( e ){
                    if( isNaN( parseFloat( this.value ) ) ) return;
                    this.value = parseFloat(this.value).toFixed(2);
                });
            });
            return this; //for chaining
        }
    })( jQuery );


    $( function() {
        $('.currency').currencyFormat();
    });​

OR ENter key & Tab key here: http://jsfiddle/Mtw7c/7/

 $('.currency').live('keydown', function(e) { 
        var key = e.keyCode || e.which; 

          if (key== 9 || key == 13) { 
            e.preventDefault(); 
              if( isNaN( parseFloat( this.value ) ) ) return;
                    this.value = parseFloat(this.value).toFixed(2);
            // call custom function here
          }         

    });

hope this helps and it will be good if you read the following link - sample taken from here but to make it sample for you, In jQuery, what's the best way of formatting a number to 2 decimal places?

You can always put validation for the number only text box & other amendments, Hope this helps mate, cheers!!

发布评论

评论列表(0)

  1. 暂无评论