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

TC

thecalculator.tech

30 June 2026 · 4 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.

Was this guide helpful?

Your feedback helps us create better content.