Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

125
Vistas
I need to have this JavaScript function only output integers
<html>

<script>


function fahrenheitToCelcius(temp) {
    return (parseFloat(temp) - 32) * (5 / 9);
}

function isNumber(value) {
    return typeof (value) != "boolean" && !isNaN(value) && value.length > 0;
}

function minMaxTemp(value, min, max, unit) {
    console.log("function called");
    if (value.length == 0 || value == "-") return value;
    if (!isNumber(value)) return value.substring(0, value.length - 1);

    if (unit == 1) value = fahrenheitToCelcius(value);

    if (parseFloat(value) < min)
        return min;
    else if (parseFloat(value) > max)
        return max;
    else return value;

}
</script>

<table class="center">
    
      <tr>
        <th>Setpoint</th>
        <th><input id="setPoint" type="text" name="setPoint" value="4" onkeyup="this.value = minMaxTemp(this.value, -80, 150, 0)"   /></th>
       
      </tr>
</table>
</html> 
  • the code is fucntioning for min and max values but it still outputs decimal point values which i dotn want it to doo ie 4.5
  • the function must only output integers ie 4 ,5, 7 not for example 4.0, 5.8, 7.6
  • below is the code with a web form and javascript function
  • thank you for any help provided
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Depending on whether you want to round down or round up, you can use Math.floor() or Math.ceil().

<th><input id="setPoint" type="text" name="setPoint" value="4" onkeyup="this.value = Math.floor(minMaxTemp(this.value, -80, 150, 0))"   /></th>

MDN docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor

about 4 years ago · Juan Pablo Isaza Denunciar

0

Since from your expected output it looks like you want the decimal numbers rounded down, I would recommend using JavaScript's Math.floor function to round the value down to the nearest integer

i.e.

Math.floor(3.712)

will return the value 3

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda