Formula for reversing digits of positive integer $n$

I was able to work out the cases for $n$ having up to $4$ digits and was wondering if someone could verify my generalization to $m$ digits. Here I am assuming that when a reversal results in there being a leading $0$ it get ignored (e.g. $76130$ gets reversed to $3167$, $998700$ to $7899$ etc.)

I believe the function can be expressed as $r(n): \mathbb{N} \rightarrow \mathbb{N}$
$$r(n) = \left \lfloor \frac{n}{10^m}\right \rfloor +\sum_{k=0}^{m-1}10^{m-k} \left( \left \lfloor\frac{n}{10^k} \right \rfloor-10 \left \lfloor \frac{\left \lfloor \frac{n}{10^k}\right \rfloor}{10}\right \rfloor \right)$$

where again, $n$ has $m$ digits.

It seems like this combination of the floor function and powers of 10 is the only way to achieve this but is this true?

Solutions Collecting From Web of "Formula for reversing digits of positive integer $n$"

Your first term is always $0$, and you’re off by a factor of $10$; for example, by your formula


instead of the correct $321$. However,


does the trick.

Suppose that $n=\sum_{i=0}^{m-1}d_k10^i$, where each $d_i\in\{0,1,\dots,9\}$, and $d_{m-1}\ne 0$, so that the decimal expansion of $n$ is $d_{m-1}d_{m-2}\ldots d_1d_0$. Then






whose decimal expansion is $d_0d_1\ldots d_{m-2}d_{m-1}$, as desired.

This is what I came up with:
Where n is the number of digits in the number x, it could be replaced with $\lfloor{log_{b}x}\rfloor+1$
I believe it works in different bases (just change 10 for the desired base), although it has only been tested in base 10! Taking the first term k = 1, with n = 3 for example gives:
The $xmod1000$ term returns the original number, the $xmod100$ term returns the remainder after division by 100 which will be the two digit number after the “number of hundreds”, the result will then be the number of hundreds eg. for 345 the numerator will be 300, dividing by 100 returns 3. The next term in the series essentially does the same thing, the fraction returns the value the next power of ten along with one important difference; the $10^{k-1}$ term makes the digit be worth ten times what it was so in the 345 example the 3 will be worth 3 instead of 300 the for will remain at the same value of 40 and the 5 will be worth 500 because $10^{3-1}$ = 100 so at the third pass the value of the fraction is equal to 100 times the value of the digit at the third place. Adding all the terms together reverses the number 345 becomes 543, 1432 becomes 2341. If my explanation isn’t clear let me know!