digits = (number == 0) ? 1 : (log10(number) + 1); //or while (number > 0) { number /= 10; digits++; } //see: https://ideone.com/P1h8Ne