Tackling this task, I'd first find the number of decimal places in x
, then round y
accordingly. I'd use:
y.toFixed(x.toString().split(".")[1].length);
It should convert x
to a string, split it over the decimal point, find the length of the right part, and then y.toFixed(length)
should round y
based on that length.