Jump to content

[HTML] Decimal places in Input Text Box


Recommended Posts

I am working on a website and I have a mortgage calculator.

I want to limit the number of decimal places in the output, is that possible?

(so instead of displaying "1234.56789" it will display "1234.57")

Thanks

Link to comment
Share on other sites


Ok adding ".toFixed(2)" works in Internet Explorer, but now firefox just doesn't display the result. Any ideas?

Sounds like you're trying to use the toFixed method on a var containing a string (even though it contains numbers), and that will throw an error instead of work, specifically "yourvarnamehere.toFixed is not a function". So try to convert the contents of the variable you fill from the inputbox to a number first (many simple ways to do that, including Number() or subtracting 0 from it).

A quick and dirty example of that (including the 2 quick & simple methods I just mentioned):

<script type="text/javascript">
var somestring = prompt("Enter a number","12.34567890")
var test1 = Number(somestring)
var test2 = somestring - 0
alert ("The number is: " + test1.toFixed(2) + " or " + test2.toFixed(2))
</script>

If you're using Firefox, you might as well take advantage of the far superior web dev tools, like the web developer toolbar, the javascript debugger, firebug and all those.

Edited by crahak
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...