ThisCalculator

Everyday

Formula for Counting Digits in a Cell

Updated · Standard formula, shown below

The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.

Formula for Counting Digits in a Cell

This calculator works out how many digits appear in a given cell of a spreadsheet, such as Excel or Google Sheets. You enter the cell reference or a text string, and you get back the count of numeric characters (0-9) within that cell. The formula uses the LEN and SUBSTITUTE functions to count digits by removing them and comparing the lengths.

Formula

The general formula to count digits in a cell (e.g., cell A1) is:

=LEN(A1) – LEN(SUBSTITUTE(A1, “0”, “”)) – LEN(SUBSTITUTE(A1, “1”, “”)) – LEN(SUBSTITUTE(A1, “2”, “”)) – LEN(SUBSTITUTE(A1, “3”, “”)) – LEN(SUBSTITUTE(A1, “4”, “”)) – LEN(SUBSTITUTE(A1, “5”, “”)) – LEN(SUBSTITUTE(A1, “6”, “”)) – LEN(SUBSTITUTE(A1, “7”, “”)) – LEN(SUBSTITUTE(A1, “8”, “”)) – LEN(SUBSTITUTE(A1, “9”, “”))

Where:

  • LEN(text) returns the number of characters in a text string.
  • SUBSTITUTE(text, old_text, new_text) replaces all occurrences of a substring with another substring.
  • A1 is the cell reference containing the text you want to analyze.

The logic: start with the total character count. For each digit (0 through 9), remove all instances of that digit using SUBSTITUTE and measure the length again. The difference between the original length and the length after removal gives the count of that specific digit. Summing these differences across all ten digits yields the total number of digits in the cell.

Worked Example

Suppose cell A1 contains the text “A1B2C3”. Using the default value from the calculator (A1 as the reference), let’s trace the steps:

  1. Original length: LEN(“A1B2C3”) = 6.
  2. Remove “0”: LEN(“A1B2C3”) = 6, difference = 0.
  3. Remove “1”: LEN(“AB2C3”) = 5, difference = 1.
  4. Remove “2”: LEN(“A1BC3”) = 5, difference = 1.
  5. Remove “3”: LEN(“A1B2C”) = 5, difference = 1.
  6. Remove “4” through “9”: each gives difference = 0.
  7. Total digits = 0 + 1 + 1 + 1 + 0 + 0 + 0 + 0 + 0 + 0 = 3.

The result is 3 digits (1, 2, and 3). The calculator with default input “A1” would compute the same if A1 contained “A1B2C3”. Note that the default in the calculator is just a placeholder; you replace it with your actual cell reference or text.

When to Use This Formula

This formula is useful in data cleaning and analysis when you need to:

  • Verify that a column of IDs, phone numbers, or codes contains the expected number of digits.
  • Extract numeric parts from mixed strings for further processing.
  • Detect anomalies in text data, such as accidentally typed letters in numeric fields.
  • Prepare data for import into systems that require strict digit counts (e.g., ISBN, SSN).

It works in Excel, Google Sheets, LibreOffice Calc, and most spreadsheet applications because the core functions (LEN and SUBSTITUTE) are standard.

How to Read the Result

The output is a single integer representing the total count of digit characters (0-9) in the given cell. For example:

  • “ABC123” → 3
  • “2024-05-01” → 8 (the hyphens are not digits)
  • “Room 404” → 3
  • “No digits here!” → 0

If your data contains decimal points or commas used as thousand separators, those are not counted as digits. Only the characters 0 through 9 are counted.

Common Mistakes and Limitations

  • Forgetting to remove all ten digits: The formula must subtract each digit individually. Omitting one digit (e.g., not subtracting “0”) will undercount.
  • Using SUBSTITUTE with a blank new_text: In Excel, SUBSTITUTE(A1, “0”, “”) works fine, but in some older versions you may need to use “” (two double quotes) to represent an empty string correctly.
  • Cell formatting: If the cell is formatted as a number, LEN will still work on the displayed text, but if the number has leading zeros (e.g., 00123), the zeros may be lost unless the cell is text-formatted. Always ensure your data is stored as text if leading zeros matter.
  • Non-ASCII digits: The formula counts only standard Western Arabic numerals (0-9). It will not count Arabic-Indic, Devanagari, or other Unicode digit characters.
  • Performance on large datasets: This formula is efficient for normal use, but if you apply it to hundreds of thousands of rows, it may slow down your spreadsheet. Consider using array formulas or scripting for very large datasets.

Alternatives and Variations

If you need to count only specific digits or use a different approach, consider these alternatives:

  • Count a specific digit: For example, to count the number of “5”s in A1: =LEN(A1)-LEN(SUBSTITUTE(A1,"5","")).
  • Using SUMPRODUCT with MID: An array formula like =SUMPRODUCT(--ISNUMBER(VALUE(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)))) counts digits but is more complex and may be slower.
  • Using REGEX in Google Sheets: =REGEXREPLACE(A1, "[^0-9]", "") removes non-digits, then LEN gives the count. This is simpler but not available in Excel.
  • Power Query (Excel): You can split the column by non-digit characters and count the resulting parts, but this is more involved.

For most users, the SUBSTITUTE method is the most straightforward and portable.

Frequently Asked Questions

Does this formula count spaces and letters?

No. The formula counts only the characters 0 through 9. Spaces, letters, punctuation, and symbols are ignored. For example, “A 1 B 2” has 2 digits.

What if my cell contains a formula that returns text?

The formula works on the result of the formula in the cell. For instance, if A1 contains =CONCATENATE("ID", 123), LEN(A1) will see “ID123” and count 3 digits.

Can I count digits across multiple cells at once?

Yes, you can sum the results for each cell. For example, to count digits in A1 and B1: =digit_count(A1) + digit_count(B1) if you have a custom function, or use the full formula twice and add them. In Google Sheets, you can also use an array formula with SUMPRODUCT.

Why does the formula give a wrong result for negative numbers?

Negative numbers like -123 have a minus sign that is not a digit, so the count is 3, which is correct. However, if the number is formatted with parentheses for negatives (e.g., (123)), those parentheses are not counted either, so the result is still 3.

Is there a simpler way to count digits in Google Sheets?

Yes, Google Sheets supports regex: =LEN(REGEXREPLACE(A1, "[^0-9]", "")) removes all non-digit characters and then counts the remaining length. This is shorter but only works in Google Sheets, not in Excel.

Does the formula work with numbers stored as text?

Yes, it works regardless of whether the cell is formatted as text or number. The formula treats the cell content as a string. Just be aware that if a number is formatted with a thousands separator (e.g., 1,234), the comma is not counted, so the digit count is 4.

Related

Formula used
in t | Text or cell reference | - | A1 | 0
out count | Number of digits | - | LEN(t) - LEN(SUBSTITUTE(t, "0", "")) - LEN(SUBSTITUTE(t, "1", "")) - LEN(SUBSTITUTE(t, "2", "")) - LEN(SUBSTITUTE(t, "3", "")) - LEN(SUBSTITUTE(t, "4", "")) - LEN(SUBSTITUTE(t, "5", "")) - LEN(SUBSTITUTE(t, "6", "")) - LEN(SUBSTITUTE(t, "7", "")) - LEN(SUBSTITUTE(t, "8", "")) - LEN(SUBSTITUTE(t, "9", "")) | 0

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.