Excuse my lack of mathematical notation, hopefully this makes sense.
Say I have a number which is x
10,000,000,000,000 > x > 999,999,999
I always want to multiply this number by some other number y, so that the result z in the thousand billions, so for example:
x = 1,230,000,000
x * y = z
z = 1,230,000,000,000
y = 1000
x = 55,123,000,000
x * y = z
z = 5,512,300,000,000
y = 100
x = 456,123,000,000
x * y = z
z = 4,561,230,000,000
y = 10
x = 7,894,560,000,000
x * y = z
z = 7,894,560,000,000
y = 1
All I have is x and the I know that x is be between 1,000,000,000 and 9,999,999,999,999.
How can I find the value of y so that I can calculate z? I'm currently doing this manually (in code) with if statements,
if x < 10 => x * 1000
if x < 100 => x * 100
if x < 1000 => x * 10
if x < 1000 => x
But I'm looking for a single mathematical function that can do this for me.
Solved with Arnaud Mortier's answer
Using the marked answer I created the function (in Javascript)
const fn = x => {
let k = 12 - Math.floor(Math.log10(x));
let y = Math.pow(10, k);
let z = x * y;
console.log(`x = ${x}, k = ${k}, y = ${y}, z = ${z}`);
return z;
};
Even more than I hoped for, this works for any positive integer, not just those in my range.
Set $y=10^{k}$. \begin{align}10^{12}\leq z<10^{13}&\Longleftrightarrow 12\leq {\log_{10}(x\cdot 10^k)}<13\\&\Longleftrightarrow \left\lfloor \log_{10}{(x\cdot 10^k)}\right\rfloor=12\\&\Longleftrightarrow \left\lfloor \log_{10}{(x)+k}\right\rfloor=12\\&\Longleftrightarrow k=12-\left\lfloor \log_{10}{(x)}\right\rfloor\\\end{align}
If you don't have $\log_{10}$ implemented in your computer, use $\log_{10}(x)=\frac{\ln(x)}{\ln(10)}$.
The function $\left\lfloor x\right\rfloor$ is usually implemented as $\operatorname{floor}(x)$.