I have an input that collects the value of height, and a function that transforms the value:
- case 1: if income 123 adds a zero (0) and the result is 123.0 THIS GOOD
- case 2: if you enter 123.4, leave it as is. IT'S GOOD
case 3: if income 99 adds a zero (0) and the result is 123.0 THIS GOOD - case 4: if I enter 1.23 I will transform it to 123.0 THIS GOOD
- But in case 5: if I enter 99.4 he transforms it to 994.00. THAT'S WRONG. I should leave it equal 99.4.
THIS IS MY FUNCTION:
function format(input){
var num = input.value.replace(/\./g,'');
if(!isNaN(num)){
if(num.length>3){
num=num.substring(0,num.length-2)+'.'+num.substring(num.length-2);
}
console.log(parseFloat(Math.round(num * 100) / 100).toFixed(1));
}
}
<input type="text" id="ejemplo" name="ejemplo"/>
<button type="button" onclick="format(ejemplo)">Prueba</button>