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

327
Vistas
How can I convert a random number to a multiple of another number?

So I'm working on this app where I need to convert random numbers to a multiple of other numbers. Let's say I have a number 0.00486585 and it needs to be a multiple of 0.00010000. The random numbers could be anything, from integers to floating point numbers but they will have their own increment values.

The increment values could be something like 1, 0.1, 0.01, 0.001, etc. Here's a statement to what an incremental value is: The increment of the order size. The value shall be a positive multiple of the baseIncrement.

I know I can check if a number is a multiple of another with the help of modulo but this is a little bit different as I need to have an universal formula which would work with any random number or any incremental values to convert a number to be a multiple.

Please note that the resulting number should be as close as possible to the original number and should not exceed the original number.

Your help would be highly appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Consider

x = 0.21321
y = 0.01
x-(x%y)

This should give you the number which is divisible. However, there is an issue here because of rounding off error in JS

x%y comes at 0.0032100000000000063

To avoid this, you can use a round off

(x-(x%y).toFixed(5))

toFixed(5) is just for reference. You should alter the decimal acccording to the requirement

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