How the Spanish DNI letter is calculated (and why it exists)

CT

Transparent Calculators

30 June 2026 · 3 min read

The letter in a Spanish DNI is not random: it is obtained by dividing the number by 23 and looking up the remainder. We explain the full algorithm, the letter table, and why this system was chosen.

When the Spanish DNI incorporated the control letter in 1990, the goal was twofold: to detect typing errors when entering the number and to make document forgery harder. The letter acts as a check digit, just like the control digit of an IBAN or the last digit of an EAN-13 barcode.

The algorithm step by step

  • •Take the DNI number (8 digits, without the letter).
  • •Divide it by 23 and keep the remainder (modulo operation).
  • •The remainder will be a number between 0 and 22.
  • •Look up that number in the 23-letter table to get the control letter.

The letter table

The official sequence is: T R W A G M Y F P D X B N J Z S Q V H L C K E. Remainder 0 corresponds to T, remainder 1 to R, remainder 2 to W… and so on up to remainder 22, which corresponds to E. The letters I, O, Ñ and U are excluded to avoid visual confusion with the digits 1, 0 and with Ü or V.

Practical example

DNI number 12345678. Divide 12,345,678 ÷ 23 = 536,768 remainder 14. Number 14 in the table corresponds to the letter Z. So the full DNI would be 12345678Z. You can verify this against any official database.

NIE: the variant for foreigners

The NIE (Número de Identidad de Extranjero) starts with X, Y or Z. To calculate the control letter, X is replaced with 0, Y with 1 and Z with 2, then the same algorithm as the DNI is applied. For example, a NIE starting with Y is treated as if it started with 1, the modulo 23 is applied and the result is looked up in the same table.

The DNI letter contains no personal information: it is purely a mathematical check digit. Two people with consecutive numbers will have completely different letters.

A complete example, step by step

Take the number 12345678. Divide it by 23: the quotient is 536,768 and the remainder 14. Look up 14 in the control table and the matching letter is Z, so the document would be 12345678Z. The calculation needs no scientific calculator: divide, keep the integer part, multiply it by 23 and subtract that product from the original number. That subtraction is the remainder, and the remainder alone determines the letter.

Why I, Ñ, O and U are excluded

Of the twenty-seven letters in the Spanish alphabet only twenty-three are used in the check, and the four missing ones are not absent by chance. I and O are dropped because by hand they are confused with 1 and 0. U is dropped for its resemblance to V. And Ñ is left out because it does not exist on many foreign keyboards, alphabets and computer systems, which would have caused problems in passports, tickets and international databases. It is a good example of a design decision made for human error rather than mathematical elegance.

What checking the letter is and is not good for

Verifying the letter catches typos when entering a number, which is why forms, invoicing software and databases include it. What it cannot tell you is whether a document really exists, whether it is valid, or whether it belongs to whoever presents it: anyone can generate a valid number-and-letter combination, because the algorithm is public. Verifying a real identity requires official verification services, and checking a document's validity requires the administration itself.

Pro Tip

If a form rejects your NIE, check that the initial letter has been replaced by its number: X becomes 0, Y becomes 1 and Z becomes 2. It is the most common failure in automated validations.

Try the calculator

Use the DNI Letter and get your personalised result in seconds.

Open calculator →

Sources & references

Keep reading

Was this guide helpful?

Your feedback helps us improve our guides.