Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: cutting float

  1. #1
    New Coder
    Join Date
    Jan 2006
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    cutting float

    i need to get a price from a user.
    the thing is, if he prints 15.545689 i want to get the number with only two digits after the floating point. so the number above will result 15.54

  • #2
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    <html>
    <head>
    <script language="javascript">

    function moneyFmt(thisNumber){
    thisNumber+="";
    for (i=0; i<thisNumber.length; i++)
    if (thisNumber.charAt(i)==".")
    return thisNumber.substring(0, i+3)
    }

    </script>
    </head>
    <body>

    <form name="cartform">
    <input type="text" name="total">
    <input type="button" onclick="alert(moneyFmt(this.form.total.value));>
    </form>
    </body>
    </html>

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You need to simply use the built-in numberVar.toFixed(2) method.
    But it is incorrectly implemented in some browsers or not implemented at all.
    liorean prototyped a similar cross-browser method which can be found in the JS FAQ.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •