Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

325
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda