How can I get the first (most significant) N digits in a positive integer, without counting the total number of digits in it?

65 Views Asked by At

I can calculate the number of digits in $x$:

$$M=\Big\lfloor\log_{10}{x}\Big\rfloor+1$$

And then calculate the first $N$ digits in $x$:

$$\Big\lfloor\frac{x}{10^{M-N}}\Big\rfloor$$

Is there any trick to achieve that without counting the number of digits in $x$ to begin with?

1

There are 1 best solutions below

0
On

$x = 12345678$

$y = log_{10}(x)$

$z = \left\lfloor y \right\rfloor$

$t=y-z$

$u= 10^t$

And then work with $u$