Why are there so many occurrences of the number 40 in the Bible (Gen 7:4,17)?

This is one of several “round numbers” to be found in the Bible. Four of many different things; ten plagues and commandments; a dozen tribes and apostles. The number 40 appears not a few, but quite many times in the Bible: days of Moses on Mount Sinai; years of wandering in the desert; days Jesus was tempted in the wilderness. Now, four often indicates completeness (as in the four directions of the compass, indicating every part of the land). So 40 would seem to be “a fullness of days,” but particularly days of grace in the face of testing. So, whether or not it was precisely 40 days that it rained, the number stated in the text does indicate that it rained fully as necessary, i.e., for as many days as were needed, to completely drown the world, and yet God saved Noah during that most perilous time.