I'm new to this forum, but I wanted to post this question hoping to know if anyone has come across it.
Is there a formula in math to round down to the closest power of ten?
For example, $n$ : the number, $r$: the round value. if $n = 101\Rightarrow r = 100$. if $n = 322\Rightarrow r = 100$. if $n = 1200\Rightarrow r = 1000$. if $n = 77\Rightarrow r = 10$.
We need this in finance business, where trades are bought in lot sizes of $10, 100, 1000$ etc.
It sounds like you have to implement the answer as a script in order to use it. Here is how it can be done in python3:
This returns the powers of ten as desired. However, there is a problem - log(1000,10) in python returns 2.999999 not 3, which 'floors' to 2. But, if you write it 10**(floor(round(log(x,10)))) you get the wrong power of ten if the log result rounds up for other values. What is the remedy for this?
EDIT:
A better technique, as suggested by Barry Cipra.