javascript - text box decimal format fixing - Stack Overflow

I have a simple text box and I am entering number value to this.And i want to format the number value t

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

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

相关推荐

  • javascript - text box decimal format fixing - Stack Overflow

    I have a simple text box and I am entering number value to this.And i want to format the number value t

    17小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信