Math / Count number of digits with Logarithm
Posted On 01.21.2022
Suppose that a number
Because
Now, take logs base 10 of this relation:
This becomes:
If you now take the integer part of
For example,
If we take the integer part and throw away everything to the right of the decimal point, it’s
The calculation can be done programmatically like this:
#include <math>
int digits = log10(n) + 1;
Source: Proof: How many digits does a number have? - StackExchange Mathematics◹