Home

ndigit

Ndigit is a mathematical function that returns the number of digits required to represent a nonnegative integer n in a given base b (the radix). The base-10 case is the usual decimal digit count, but the concept generalizes to any base b ≥ 2. Formally, for n ≥ 0, ndigit_b(n) = floor(log_b(n)) + 1, with the convention ndigit_b(0) = 1. Equivalently, ndigit_b(n) = floor( ln(n) / ln(b) ) + 1 for n > 0.

Examples: ndigit_10(0) = 1, ndigit_10(7) = 1, ndigit_10(42) = 2. In base 2, ndigit_2(5) = 3 because 5 is 101

Generalization and conventions: negative integers can be handled by applying the function to their absolute value,

Computational aspects: in programming, ndigit_b(n) can be computed with logarithms or by repeated division by b.

Applications: digit counting is used in numeric formatting, data encoding, indexing, and algorithms that depend on

See also: number of digits, digit count, logarithm, radix.

in
binary.
i.e.,
ndigit_b(n)
=
ndigit_b(|n|).
Leading
zeros
do
not
affect
the
count,
and
the
value
increases
by
one
when
n
reaches
a
new
power
of
b
(for
example,
in
decimal,
at
10,
100,
1000,
and
so
on).
The
logarithmic
method
runs
in
constant
time
for
fixed-precision
arithmetic
but
relies
on
floating-point
operations;
the
division
method
runs
in
O(log_b
n)
time
and
uses
only
integer
arithmetic.
the
magnitude
of
numbers.
It
also
provides
a
basis
for
understanding
numeral
systems
and
base
conversions.