[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4985
  • Last Modified:

Javascript add decimal point automatically (.00) to a field after number is entered

I have a text field where users enter whole numbers.  What I want to do is to automically add a decimal point and two zeros to the end of the number (ex: 123.00) when it is manually entered.  For some reason, it is too much work for users to simply add .00 manually.

Anyway, any assistance is greatly appreciated. Thank you.  
0
arendt73
Asked:
arendt73
  • 3
1 Solution
 
HonorGodCommented:
Use toFixed( 2 )

value.toFixed( 2 )

makes sure that there are 2 places after the decimal point
0
 
arendt73Author Commented:
Thank you.  Where will this be placed in my code?
<tr> 
      <td><strong>Amount Entered:</strong></td>
      <td> <input name="amount" type="text" id="amount" size="6" maxlength="8">
        <span class="text-small"></span></td>
    </tr>

Open in new window

0
 
HonorGodCommented:
Something like this perhaps?

<input name="amount" type="text" id="amount" size="6" maxlength="8" onchange='fixit(this)'>

<script type='text/javascript'>
  function fixit( obj ) {
    obj.value = parseFloat( obj.value ).toFixed( 2 )
  }
</script>

Open in new window

0
 
HonorGodCommented:
Thanks for the grade & points.

Good luck & have a great day.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now