VANGOOSE
VANGOOSE

Reputation: 55

How to limit the digits behind the decimal to 2?

I just started to learn how to code websites just for fun. I am pretty OK with HMTL and CSS but I have a very hard time to understand JS.

As a learning project I am working on a simple units calculator. I am using a number input bar and I would like the users entry to be restricted to 2 digits after the decimal and the result display (same bar but from different function will also be restricted to 2 digits after the decimal).

I know many people asked this question here but i just couldn't make these examples work as I am new to this. If its possible I would like to ask your help.

Many thanks in advance.

This is the HTML:

<form action="" method="" name="vform">
 <input type ="number" min="0" max="10000000" 
  placeholder="PEEM it!" value="" id="footbar"  />
  <div id="foot2">
   <li class="foot" alt="foot"  title="FOOT (ft)" onclick="runfoot()"><a  
     href=""></a></li>

Upvotes: 0

Views: 1126

Answers (2)

vicatcu
vicatcu

Reputation: 5837

you can call .toFixed(2) on a numeric variable to turn it into a string with two digits after the decimal point.

x = 3.35563637;
y = x.toFixed(2);
y === "3.36"
+y === 3.36

Upvotes: 1

mullac
mullac

Reputation: 683

Try adding step="0.01" to your number input like so:

<form action="" method="" name="vform">
  <input type ="number" min="0" max="10000000" placeholder="PEEM it!" value="" id="footbar" step="0.01"/>
  <div id="foot2">
   <li class="foot" alt="foot"  title="FOOT (ft)" onclick="runfoot()"><a href=""></a></li>

Upvotes: 0

Related Questions