I have a given number: $N$, and I would like to explicitly round it to the nearest 2 significant digits
I have the following working equation that seems very redundant to me: $$\begin{align*} x &= \left\lfloor\log(N)\right\rfloor-1\\ y &= N 10^{-x}\\ \text{Answer} &=\left\lfloor y + (y \bmod 1)\right\rfloor 10^{x} \end{align*}$$ I chose to use the $Floor$ operation here because I know that I can explicitly break down any floor function into $$\lfloor x\rfloor = x - (x\bmod1)$$
I know that calculating $x$ is probably necessary because I need to find the position of second most significant digit, but I can't help but feel that there is a much simpler way of performing the rounding as dropping down to a tens' digit and raising it back to it's proper position seems rather redundant to me.
Thank you in advance for all of the help
Take a number like $5.678758$. Then multiply it by $100$ to get $567.8758$. You can see that it places the two decimal places in the integer area now, so that only the ones place, which was the original number's hundredths place, will be affected by the round function, which is what happens when you round something to two decimal places.
The round function will make it so that when when the tenths place of our new number is $5$ or above $5$, the ones place of our number increases by $1$, otherwise, it doesn't change. This is exactly what we want, because we want a number like $5.687$ to round up to $5.69$, and a number like $5.683$ to round down to $5.68$. So, then use round() function on this number to get $568$. Now divide by $100$ to get $5.68$.
So, in short you want to program a function like this for a number $x$: $f(x) = round(100x)/100$.