0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Count Digits in a Number

Use ($number.ToString()).Length in PowerShell to count the digits of a number by converting it to a string and measuring its length.
📋

Examples

Input12345
Output5
Input0
Output1
Input-9876
Output4
🧠

How to Think About It

To count digits in a number, think of the number as a string of characters. Convert the number to a string, then count how many characters it has, ignoring any sign like minus.
📐

Algorithm

1
Get the input number.
2
Convert the number to a string.
3
If the string starts with a minus sign, remove it.
4
Count the length of the remaining string.
5
Return the count as the number of digits.
💻

Code

powershell
function Count-Digits {
    param([int]$number)
    $str = $number.ToString()
    if ($str.StartsWith('-')) {
        $str = $str.Substring(1)
    }
    return $str.Length
}

# Example usage
$inputNumber = -12345
Write-Output "Number of digits in $inputNumber is $(Count-Digits $inputNumber)"
Output
Number of digits in -12345 is 5
🔍

Dry Run

Let's trace the number -12345 through the code

1

Convert number to string

Input: -12345 -> String: "-12345"

2

Check for minus sign

String starts with '-', so remove it -> "12345"

3

Count length

Length of "12345" is 5

StepString ValueLength
After conversion-123456
After removing '-'123455
💡

Why This Works

Step 1: Convert number to string

Using ToString() turns the number into text so we can count characters.

Step 2: Remove minus sign

If the number is negative, the minus sign is not a digit, so we remove it with Substring(1).

Step 3: Count characters

The length of the string after removing the sign is the count of digits.

🔄

Alternative Approaches

Using math and loops
powershell
function Count-Digits {
    param([int]$number)
    $num = [math]::Abs($number)
    if ($num -eq 0) { return 1 }
    $count = 0
    while ($num -gt 0) {
        $num = [math]::Floor($num / 10)
        $count++
    }
    return $count
}

Write-Output "Digits: $(Count-Digits -12345)"
This method uses math to count digits without converting to string, which can be faster for very large numbers.
Using regex to remove non-digits
powershell
function Count-Digits {
    param([int]$number)
    $str = $number.ToString() -replace '[^0-9]', ''
    return $str.Length
}

Write-Output "Digits: $(Count-Digits -12345)"
This method removes any non-digit characters using regex before counting length, useful if input may contain other symbols.

Complexity: O(n) time, O(n) space

Time Complexity

Converting the number to a string and counting its length takes time proportional to the number of digits, so O(n).

Space Complexity

The string conversion requires extra space proportional to the number of digits, so O(n).

Which Approach is Fastest?

The math-based loop approach uses O(1) extra space and is efficient for very large numbers, while string methods are simpler and fast enough for typical use.

ApproachTimeSpaceBest For
String lengthO(n)O(n)Simplicity and typical use
Math loopO(n)O(1)Large numbers and memory efficiency
Regex filterO(n)O(n)Input with extra symbols
💡
Convert the number to a string and count characters for a simple digit count.
⚠️
Counting the minus sign as a digit instead of removing it first.