0

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>
mullac
  • 693
  • 6
  • 17
VANGOOSE
  • 55
  • 6

2 Answers2

1

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
vicatcu
  • 5,407
  • 7
  • 41
  • 65
  • thank you , i never used it before . can you please show how to actually use it – VANGOOSE Apr 02 '19 at 01:50
  • @VANGOOSE added with usage... also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed – vicatcu Apr 02 '19 at 03:06
0

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>
mullac
  • 693
  • 6
  • 17