1

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.net or asp.net 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);         

      }
    };
};
Adeel Aslam
  • 1,285
  • 10
  • 36
  • 69
  • 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 Mar 20 '12 at 07:01

2 Answers2

2
var num = 10;

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

Examples

dhinesh
  • 4,676
  • 2
  • 26
  • 44
  • Sir is there any way to fix the textbox for Enter or tab event because when i press the enter it format the all textboxes on page with toFixed function – Adeel Aslam Mar 20 '12 at 07:30
  • @user1220667 Hiya, use this for your key events this should help: http://stackoverflow.com/questions/2335553/jquery-how-to-catch-enter-key-and-change-event-to-tab - see my answer below I have include js fiddle for your enter key as well cheers! – Tats_innit Mar 20 '12 at 07:41
  • @user1220667: You have attached the keydown event to the document instead attach to the textbox as shown by Tats_innit. – dhinesh Mar 20 '12 at 08:03
  • @dhinesh cheers mate, lol I actually made everything for the dude below ha ha - o well time to sleep for me soon then :P, cheers! – Tats_innit Mar 20 '12 at 08:05
  • How can i attach with textbox please tell me ? – Adeel Aslam Mar 20 '12 at 09:03
  • @user1220667 have you actually read my reply below? it has link code and everything you need; comment on it and I am happy to guide you to get this thing sorted and bail out, cheers! – Tats_innit Mar 20 '12 at 09:19
  • Sir I dnt understand that code and also that is not working in my JS code please only tell me how to attach Enter key with a tect box / – Adeel Aslam Mar 20 '12 at 09:31
  • @user1220667 you can copy the code from jsfiddle and understand it but anyways: can you provide more code if you want help i.e. HTML and things, My sample below is a mock because of the missing html and thing you got, Paste your html in comment here! cheers – Tats_innit Mar 20 '12 at 09:40
  • Yes Please chek my above JS code in which i doing something with txtRatelb and txtRatelb textboxes problem is that the enter key is affecting on both textboxes but i want to specify the enter key for each of them so when i press enter key on textbox then it should be call code and other text dnt affected by enter. – Adeel Aslam Mar 20 '12 at 09:47
  • @user1220667 okies - can you do this please - add html attribute - class="currency" in your text boxes and then **copy** my second **Enter and Tab key function from below answer or from http://jsfiddle.net/Mtw7c/8/**, and paste/put it inside document.ready and that should do the trick and you can remove your old function? does this sound good? – Tats_innit Mar 20 '12 at 09:54
  • Sir I am using Textbox not input .and also i have added class="currency" with text box then paste jquery code from fiddle but this make no any effect on text box – Adeel Aslam Mar 20 '12 at 10:03
  • @user1220667 Are you suer that you cannot put class attribute in your textbox? – Tats_innit Mar 20 '12 at 10:08
  • Sir I am Sure there is class for textbox but it is Cssclass not class.But Cssclass="currentcy" is also not working in jquery code – Adeel Aslam Mar 20 '12 at 10:10
  • @user1220667: CssClass is server attribute, however when it is rendered in browser then it becomes class. So you can go ahead and add "currency" in CssClass – dhinesh Mar 20 '12 at 10:13
  • @user1220667 Yes I am talking about class attribute; anywho, if you can do that then it will be great but I have written another jsfiddle: http://jsfiddle.net/Mtw7c/10/ for you specific text box id - http://jsfiddle.net/Mtw7c/10/ now use the Script code in document ready and try with your "txtpkrusd" text box – Tats_innit Mar 20 '12 at 10:13
  • @user1220667 let me know once done and working and accept the answer below; cheers! – Tats_innit Mar 20 '12 at 10:18
  • view My js file AND ALSO PAGE HERE http://202.125.144.34:81/js/calc.js http://202.125.144.34:81/calc.ASPX – Adeel Aslam Mar 20 '12 at 10:40
  • please press reset fields and the enter the value in first text box then press enter – Adeel Aslam Mar 20 '12 at 10:45
  • @user1220667 interesting can you put that .live function inside document ready ? http://www.learningjquery.com/2006/09/introducing-document-ready ; [code]$(document).ready(function() { //paste here...});[code] – Tats_innit Mar 20 '12 at 10:49
  • @user1220667 weird - Can you try just putting alert inside document.ready and remove the .live functionality. just for a check, Will help you to nut this one out mate don't worry – Tats_innit Mar 20 '12 at 10:58
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9084/discussion-between-user1220667-and-tats-innit) – Adeel Aslam Mar 20 '12 at 11:02
1

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

demo (enter the number and press enter) : http://jsfiddle.net/6wG26/5/ OR Enter and Tab keys both here http://jsfiddle.net/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.net/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!!

Community
  • 1
  • 1
Tats_innit
  • 33,991
  • 10
  • 71
  • 77