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

Posted on 2009-04-29
Last Modified: 2012-05-06
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.  
Question by:arendt73
    LVL 41

    Expert Comment

    Use toFixed( 2 )

    value.toFixed( 2 )

    makes sure that there are 2 places after the decimal point

    Author Comment

    Thank you.  Where will this be placed in my code?
          <td><strong>Amount Entered:</strong></td>
          <td> <input name="amount" type="text" id="amount" size="6" maxlength="8">
            <span class="text-small"></span></td>

    Open in new window

    LVL 41

    Accepted Solution

    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 )

    Open in new window

    LVL 41

    Expert Comment

    Thanks for the grade & points.

    Good luck & have a great day.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now