-2

In the below code i have a textbox when i try to enter a number (ie)10 it should convert it to 10.00.But i tried it doesn't work for me and onblur is also not working.pls help me to solve the issue.

<script>
function sample(){

    var num = 10;
    var result = num.toFixed(2);
}</script>

<asp:TextBox ID="value"  onblur="sample(this)" ></asp:TextBox>
user3224577
  • 37
  • 1
  • 1
  • 9

6 Answers6

0

you are missing this

function sample() {

              var num = parseFloat(document.getElementById("value").value);//read the textbox value
            var result = num.toFixed(2);
            document.getElementById("value").value=result;//it will bind the result to the textbox


        }
Myth
  • 446
  • 7
  • 18
0

try this

<script>
function sample(num){

    var result = num.toFixed(2);
    document.getElementById('value').value=result; 
}</script>

<asp:TextBox ID="value"  onblur="sample(this.value)" ></asp:TextBox>
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
0

it's must be converted review your code try this example it's so close to your code W3schools Example

0

try this

parseFloat(num).toFixed(2);

this will 100% works for me..

Bhavik Patel
  • 1,466
  • 1
  • 12
  • 28
0

Try this, It works fine as you need,

            function sample() {
                var num = parseInt(document.getElementById("value").value);
                var result = num.toFixed(2);
                document.getElementById("value").value = result;
            }
Sam1604
  • 1,459
  • 2
  • 16
  • 26
0

Try the following, It might be helpful for you.

<script type="text/javascript">

$("#number").each(function(){
  $(this).val(parseFloat($(this).val()).toFixed(2));
});

</script>  
Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73