numDigits

Calculates number of digits of the provided number including sign character for negative numbers.

pure @safe nothrow @nogc
int
numDigits
(
T
)
()
if (
isIntegral!T
)

Examples

// ctfe tests
static assert(numDigits(0) == 1);
static assert(numDigits(11) == 2);
static assert(numDigits(-1) == 2);

static assert(numDigits(int.min) == 11);
static assert(numDigits(int.max) == 10);
static assert(numDigits(long.min) == 20);
static assert(numDigits(long.max) == 19);
static assert(numDigits(ulong.min) == 1);
static assert(numDigits(ulong.max) == 20);
// uint.max = 4_294_967_295 -> 10 digits
// ulong.max = 18_446_744_073_709_551_615 -> 20 digits
// long max = 9_223_372_036_854_775_807 -> 19 digits

// rt tests
assert(numDigits(0) == 1);
assert(numDigits(11) == 2);
assert(numDigits(-1) == 2);
assert(numDigits(-123) == 4);

assert(numDigits(int.min) == 11);
assert(numDigits(int.max) == 10);
assert(numDigits(long.min) == 20);
assert(numDigits(long.max) == 19);
assert(numDigits(ulong.min) == 1);
assert(numDigits(ulong.max) == 20);

long n = 10;
foreach (i; 0..20)
{
    import std.math : pow;
    assert(numDigits(pow(10UL, i)) == i+1);
}

Meta