Work

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.

DNI or NIE number

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.

Frequently asked questions

Yes. NIE numbers begin with X, Y or Z. Before applying modulo 23, that letter is converted to 0, 1 or 2 respectively. So an NIE starting with Y is treated as if the number started with 1, and the exact same algorithm as for the DNI is applied. This calculator automatically detects if you enter a NIE and performs the substitution for you.

Because the control table has exactly 23 characters (TRWAGMYFPDXBNJZSQVHLCKE). The remainder of dividing any number by 23 always gives a value from 0 to 22, which maps to a unique letter. 23 was chosen because it is prime, which minimises the probability of two different numbers sharing the same verification letter.

If the letter on your physical DNI does not match the one the tool calculates, check that you have typed all the digits correctly — a typo is the most common cause. If the discrepancy persists and you are sure of the number, the document may contain an error; in that case, visit your local Electoral Census Office or police station to request a correction.

The control table TRWAGMYFPDXBNJZSQVHLCKE deliberately omits the letters I, Ñ, O and U. I and O are dropped because they are visually confused with the digits 1 and 0, which is especially problematic on scanned or handwritten documents. U is excluded for its resemblance to V, and Ñ because it does not exist on many international keyboards and computer systems, which would cause compatibility problems. The result is 23 unambiguous letters — a number that is also prime, making it ideal for the modulo algorithm.

No. For a given DNI number, the letter is unique and mathematically determined by the remainder of dividing that number by 23. In other words, the letter carries no new information and identifies nobody: it is purely a check digit used to detect transcription errors. If someone mistypes a figure, the letter will most likely stop matching and the system will reject the entry before storing it. That is exactly the purpose it was designed for in 1990, when the letter was added to the Spanish National Identity Document.

For a Spanish individual, the NIF (Tax Identification Number) is the DNI number followed by its control letter, so in practice they are the same identifier used in different contexts. NIF is the term the tax authority uses in fiscal matters, while DNI refers to the physical document. For foreign residents the equivalent identifier is the NIE, and companies and legal entities have their own NIF starting with a letter that indicates the legal form (A for public limited companies, B for limited companies, G for associations, among others).

Related guide

Related calculators