Calculator for Spain. The rates, thresholds and reference prices used here come from Spanish legislation and market data, so the result does not apply to other countries.
Spanish DNI Letter Calculator
Enter the numbers of your Spanish DNI or NIE and get the correct check letter instantly. Calculation is done in your browser.
The letter on a Spanish DNI is not random: it is a check digit calculated mathematically from the number. Its purpose is to detect errors in number entry (as with bank IBANs or book ISBNs). The algorithm applies modulo 23 to the number and the result is converted to a letter using the official table TRWAGMYFPDXBNJZSQVHLCKE.
This calculator also works for the NIE (Número de Identidad de Extranjero), which begins with X, Y or Z. Before applying the modulo, those letters are replaced by 0, 1 and 2 respectively. The result is instant and the calculation is done in your own browser, without sending any data to any server.
This tool is only for verifying the letter. For any official matters related to the DNI, contact the relevant authorities.
How is the letter calculated?
The DNI number is divided by 23 and the remainder is taken. That remainder (0 to 22) corresponds to a letter according to the official table TRWAGMYFPDXBNJZSQVHLCKE. For the NIE, the initial letter X, Y or Z is replaced by 0, 1 or 2 before calculating.
The DNI letter algorithm step by step
The calculation is based on a modulo operation, very common in check-digit systems. The procedure has three steps. First, take the full DNI number, without the letter, as an integer of up to eight digits. Second, divide that number by 23 and keep only the remainder, which will necessarily be a value between 0 and 22. Third, use that remainder as an index into the control string TRWAGMYFPDXBNJZSQVHLCKE, counting from position 0. The letter at that position is the DNI letter. In the case of the NIE, the leading letter X, Y or Z is first replaced by 0, 1 or 2 and then the same procedure is applied.
Worked example
Take the number 12345678. We divide it by 23: 12345678 / 23 = 536768 with a remainder of 14, since 536768 × 23 = 12345664 and 12345678 − 12345664 = 14. Now we look up position 14 in the string TRWAGMYFPDXBNJZSQVHLCKE, counting from zero: T(0), R(1), W(2), A(3), G(4), M(5), Y(6), F(7), P(8), D(9), X(10), B(11), N(12), J(13), Z(14). The letter is Z, so the complete DNI would be 12345678-Z.
Control table: remainder and matching letter
Remainder 0
T
Remainder 1
R
Remainder 2
W
Remainder 3
A
Remainder 4
G
Remainder 5
M
Remainder 6
Y
Remainder 7
F
Remainder 8
P
Remainder 9
D
Remainder 10
X
Remainder 11
B
Remainder 12
N
Remainder 13
J
Remainder 14
Z
Remainder 15
S
Remainder 16
Q
Remainder 17
V
Remainder 18
H
Remainder 19
L
Remainder 20
C
Remainder 21
K
Remainder 22
E
What the letter is actually for
The DNI letter is a check digit, not identifying data. Its only function is to detect errors when typing or transcribing the number, and it does so remarkably well: if you get a single digit wrong, the probability that the letter still matches is roughly 1 in 23, around 4%. That means the system catches about 96% of single-digit errors before they reach a database. This is why forms at banks, public administrations and online shops validate the letter as soon as you enter the number. Bear in mind, however, that validating the letter only confirms the number is internally consistent, not that it belongs to a real person.
Why 23 and not some other number
The DNI letter is a check digit, and 23 was not chosen at random. Being prime, the 23 possible remainders spread evenly across all document numbers, so no letter is much more frequent than another. A prime divisor also makes the system detect the two commonest human errors when copying a number: getting one digit wrong and swapping two adjacent digits. With 23 remainders, the probability that a mistyped number happens to keep the correct letter is about one in twenty-three, a little over four per cent. That is why the system is good at catching typos but is not a security mechanism: anyone can compute the letter for a given number, which is exactly what this tool does. The letter does not validate that the document exists, only that number and letter are consistent with each other.
The NIE and other cases that do not simply follow the rule
The NIE, the foreigner identity number, uses the same algorithm with one preliminary step: it starts with the letter X, Y or Z, which must be replaced by 0, 1 or 2 respectively before dividing by 23. So a NIE beginning with Y is computed as if 1 were its first digit. That detail is why many automated checks fail with foreigners' documents. It is also worth knowing that the letters I, Ñ, O and U are never used in the control table, precisely to avoid confusion with the digits 1 and 0 and with other letters when read by hand. And a surprising case: there is no such thing as a DNI for legal entities, because companies are identified by the CIF, now integrated into the NIF, which uses a different control system and can end in either a number or a letter depending on the type of entity.