David sins by taking a census, chooses a consequence that is stayed at Jerusalem, then buys a threshing floor to build an altar to God, which ultimately becomes the site for the Temple: 2 Samuel 24:1–9; 1 Chronicles 21:1–6; 2 Samuel 24:10–17; 1 Chronicles 21:7–17; 2 Samuel 24:18–25; 1 Chronicles 21:18–22:19
The site we now know as the Temple Mount in Jerusalem was once a threshing floor. David offered to buy it from its owner, Araunah, who was prepared to give the property and whatever was needed for burnt offerings for the king.
1 Chronicles 21:24 But King David replied to Araunah, ‘No, I insist on buying it for the full price. I will not take what is yours and give it to the Lord. I will not present burnt offerings that have cost me nothing!’
Once it was used as an altar, it was also unlikely the place could have ever been used as a threshing floor again.
Although God had given him the title of king, David had to fight for it every step of the way. In fact, the only thing David ever took that wasn’t his was Bathsheba, and that didn’t work out too well. The experience would have had to have humbled him, and you can see that in his comments as he fled at the start of Absalom’s rebellion.
So by the time it came to sacrificing burnt offerings and peace offerings, David knew he had to pay for the materials.