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.



x = 0.21321
y = 0.01

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


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