2 Samuel 24:10
And David’s heart smote him after that he had numbered the people. And David said unto the LORD, I have sinned greatly in that I have done: and now, I beseech thee, O LORD, take away the iniquity of thy servant; for I have done very foolishly.
The census was driven by pride, ambition, and self-sufficiency. In prosperity David lost the humility and trust in God that had marked his earlier life, took credit to himself for the kingdom's growth in numbers, wealth, and greatness, and yielded to the devil's temptation to count Israel so he could contrast its former weakness when he took the throne with its prosperous condition under his rule. Numbering Israel was contrary to God's express command and was not acting according to God's order. The act would teach Israel to depend on its own strength and on strength of numbers instead of on the living God.
The effort to count the people represents the selfish attempt to claim or restrict God's gifts for oneself or one's own group. David became convicted that the census was wrong before the numbering was fully completed. He recognized the course as a grave sin and a very unwise act. He asked the Lord to remove his guilt. He humbled himself before God and admitted he was wrong.
In this humility he was unlike those who resist correction. His repentance was genuine but came too late to stop judgment, because the decree had already gone forth and he still had to face God's offered choice of punishments. God could not simply pass over the sin, because Israel had to learn that God was its unseen Leader and that He is both just and merciful. The verse warns against actions that bring trouble prematurely or obstruct the work God would have advanced.