Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a 0 before decimal entered in input?

I'm attempting to finish up a quick form using jQuery that needs to add a 0 before a decimal point if the decimal is the first char entered in the input.

For example, .25 would become 0.25 before the form is submitted. However, 2.05 would stay as 2.05, and no 0 would be added.

Is there a simple function here that could help me out? I'd rather not write something long and detailed if it's not necessary.

Also, here is the input box that I am asking for help with, for reference

<input type="number" name="dailygain" id="dailygain" />
like image 206
aaronhuisinga Avatar asked Dec 28 '22 00:12

aaronhuisinga


1 Answers

You can use parseFloat function to format float numbers.

var el = document.getElementById("dailygain");
el.value = parseFloat(el.value);
like image 128
VisioN Avatar answered Jan 05 '23 21:01

VisioN