It turns out that he was king for 40 years and six months. Why does verse 4 say only 40?
Radak: The Klal (total number) omits the excess, for it is not a full year. In Melachim I, 2:11, it is omitted even in the Prat (his reign in Chevron alone).
Radak citing R. Yitzchak in Yerushalmi Rosh Hashanah 1:1: The six months are lacking from the 33 years in Yerushalayim. For the honor of Yerushalayim, the half year there is counted like a full year.
Radak and Malbim (4) citing R. Yudan in Midrash Shmuel 1 26:5: A large amount swallows a small [remainder. This is why it says only 40 years.]
Radak citing R. Yudan in Yerushalmi Rosh Hashanah 1:1: For six months, Yo'av and Yisrael fought Edom (based on David's command). Hashem said "Al Tisgaru Vam" (Devarim 2:5)! Therefore, they are not counted towards his reign.
Radak based on Yerushalmi Rosh Hashanah 1:1: The six months that David fled from Avshalom, if he was Chayav Chatas, he would bring the Korban of a commoner, so they are not counted towards his reign.
Sanhedrin 107b: Verse 4 does not count six months that David was a Metzora.
Radak cites Yerushalmi Rosh Hashanah 1:1; in our text there, Yehudah Bei Rebbi said so. (PF)