Spanish DNI, NIE and NIF: How the Control Letter Works
Every Spanish identity and tax number ends in a check character. For the DNI and the NIE it is a letter worked out from the digits with a short calculation that the Ministerio del Interior publishes. This guide explains the three kinds of number, the calculation step by step and what a correct letter does and does not tell you.
Three Names for Related Numbers
NIF stands for número de identificación fiscal, the tax identification number. For most people the NIF is not a separate number: Spanish citizens use the number of their DNI, the national identity document, and foreign residents use their NIE, the identity number for foreigners. People and organisations without either receive a NIF from the Agencia Tributaria, the tax agency.
| Number | Who has it | Form | Example |
|---|---|---|---|
| DNI (also their NIF) | Spanish citizens | 8 digits + control letter | 12345678Z |
| NIE (also their NIF) | Foreign nationals | X, Y or Z + 7 digits + control letter | X1234567L |
| NIF K, L or M | People without a DNI or NIE (K: Spaniards under 14 living in Spain, L: Spaniards living abroad, M: foreigners without an NIE) | K, L or M + 7 characters + check letter | – |
| NIF of an entity | Companies, associations, public bodies and other entities | Letter for the legal form + 7 digits + control character | – |
The K, L and M numbers are set out in the general tax regulation (Real Decreto 1065/2007, articles 19 and 20), which describes them as a letter, seven alphanumeric characters and an alphabetic check character. The DNI regulation (Real Decreto 255/2025, article 12) only says that the card shows the number and its check character; the calculation itself is published on the Ministerio del Interior's website.
How the DNI Letter Is Calculated
The control letter is the remainder of the number divided by 23, written as a letter:
- Take the 8 digits of the DNI as one number.
- Divide it by 23 and keep the remainder, a whole number from 0 to 22.
- Look the remainder up in the table.
| Remainder | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Letter | T | R | W | A | G | M | Y | F | P | D | X | B |
| Remainder | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | |
| Letter | N | J | Z | S | Q | V | H | L | C | K | E |
Worked example (the Ministerio del Interior's own): 12345678 ÷ 23 = 536,768 with a remainder of 14, because 23 × 536,768 = 12,345,664 and 12,345,678 − 12,345,664 = 14. Remainder 14 is Z, so the full DNI is 12345678Z.
In code this is a single line: the letter is "TRWAGMYFPDXBNJZSQVHLCKE"[number % 23]. Only 23 letters are used, so I, Ñ, O and U never appear as a control letter. If one of them shows up at the end of a DNI, something was mistyped.
The NIE: X, Y and Z First
An NIE starts with a letter, X, Y or Z, followed by 7 digits and the control letter. Before the calculation, the first letter is replaced by a digit: X by 0, Y by 1 and Z by 2. The result is an 8-digit number, and from there the steps are the same as for the DNI.
Worked example: X1234567 becomes 01234567. 1,234,567 ÷ 23 leaves 19 (23 × 53,676 = 1,234,548), and 19 is L, so the NIE is X1234567L. The same digits with Y (11234567) or Z (21234567) give different letters, which is why the prefix must be copied exactly.
What the Letter Catches, and What It Cannot Prove
The letter is a check against typing mistakes. Because 23 is a prime number larger than any single digit, changing one digit always changes the remainder, so a single wrong digit always produces a different letter. Swapping two neighbouring digits changes the number by a multiple of 9 times a power of ten, which 23 does not divide either, so that mistake is caught as well. Larger mistakes, such as two wrong digits, can occasionally lead back to the same letter.
What the letter cannot do is show that a number exists. Every 8-digit number has exactly one correct letter, so millions of numbers that were never issued pass the check. Only the authority that issues a number can confirm it: the Ministerio del Interior for the DNI and the NIE, and the Agencia Tributaria for the other NIFs.
K, L, M and Company NIFs
The general tax regulation gives the structure of the K, L and M numbers but not how their check letter is calculated. For companies and other entities, Orden EHA/451/2008 sets the structure: a letter for the legal form, a random seven-digit number and a control character. The order lists the letters but does not publish the calculation of the control character either. The LeedyBox validator therefore recognises these numbers and names the legal form, without checking their last character.
| First letter | Legal form (Orden EHA/451/2008) |
|---|---|
| A | Sociedades anónimas (public limited companies) |
| B | Sociedades de responsabilidad limitada (private limited companies) |
| C, D | Sociedades colectivas, sociedades comanditarias (partnerships) |
| E | Comunidades de bienes, herencias yacentes and other entities without legal personality |
| F, G, H, J | Cooperatives, associations, owners' communities, civil partnerships |
| P, Q, R, S | Local corporations, public bodies, religious institutions, state and regional administration bodies |
| U, V | Temporary business associations (UTE), other types |
| N, W | Foreign entities, permanent establishments of non-resident entities |
Writing and Storing the Numbers
- Write the letter in capitals and without spaces:
12345678Z. Many forms also accept a hyphen before the letter; strip spaces, dots and hyphens before you compare two numbers. - Keep the DNI as text, not as an integer. A spreadsheet or database that stores it as a number drops leading zeros and loses the letter.
- Validate the letter on input to catch typos early, but do not treat a correct letter as identity verification.
- A DNI or NIE identifies a person. Treat it as personal data: do not put it in URLs, logs or analytics.
Test Numbers for Development
Forms and databases often need sample numbers that pass validation. The validator can generate random DNI and NIE numbers with a correct letter. They are only for testing: a random number may coincide with a real person's number, so never use generated values as anyone's identity in production data, invoices or documents.
Frequently Asked Questions
Is the NIF the same as the DNI?
For Spanish citizens, yes: the NIF is the DNI number followed by its letter. Foreign residents use their NIE as NIF, and people without either receive a K, L or M number from the tax agency.
What is the CIF?
CIF is the old name for the tax number of companies. Today companies have a NIF, made up of a letter for the legal form, seven digits and a control character.
Why is my DNI letter rejected?
Check each digit and the letter against the card. A letter such as I, Ñ, O or U is never valid, and swapping two digits always changes the expected letter.
Can I find out whose number it is?
No. The letter is calculated only from the digits; it contains no information about the holder, and the validator cannot look a number up anywhere.
Sources
Source: Ministerio del Interior: Cálculo del dígito de control del NIF/NIE (Internet Archive, 2025-12-30) · Last checked:
Source: BOE: Real Decreto 255/2025, de 1 de abril, por el que se regula el Documento Nacional de Identidad · Last checked:
Source: BOE: Real Decreto 1065/2007 (Reglamento General de gestión e inspección tributaria), artículos 19 y 20 · Last checked:
Source: BOE: Orden EHA/451/2008 (texto consolidado, modificada por la Orden HAP/5/2016) · Last checked: