There's no easy formula for this off the top of my head, but it shouldn't be hard to derive one.
First, for any three-digit decimal number ABC (i.e., 100 * A + 10 * B + C, where A,B,C are 0 through 9) let's find a way to count the number of times a specific digit appears when printing the numbers 1 through ABC.
For instance, let's count the number of "5's" used to print all the decimal numbers 1 through ABC.
Now a "5" can appear in the ones place, the tens place, or the hundreds place-- so let's count each seperately.
We first note that the numbers 1 through 10 contain one number which has a "5" in the ones place (specifically, the number 5 itself). Similarly, the numbers 11 through 20 contain exactly one number which has a "5" in the ones place (namely, the number 15). In fact, each of the following sets of ten consecutive numbers has exactly one number with a "5" in the ones place. There are precicely 10 * A + B such full sets, so there are at least 10 * A + B "5"'s used in the ones place. I say "at least", because the numbers AB0 through ABC may contain an additional number which ends in 5-- in fact, if C is 5, 6, 7, 8, or 9 then it does. Hence, the total number of "5"s that appears in the ones place is 10 * A + B if C < 5, and 10 * A + B + 1 if C >= 5.
Now we count the number of "5"'s that appear in the tens place. We note that each set of numbers 1 - 100, 101-200, etc. each contain exactly 10 numbers which have a "5" in the tens place (for instance, 50-59 and 150-159). There are A such sets, so there are at least 10 * A "5"'s that appear in the tens place. Similiar to above, between the numbers A00 and ABC there may be some additional numbers that have a "5" in the tens place. Specifically, if B > 5, then there are 10 such numbers. If B = 5, then there are C + 1 such numbers. If B < 5, there there are no additional numbers. Hence, the total number of "5"'s that appear in the tens place is 10 * A if B < 5, 10 * A + C + 1 if B = 5, and 10 * A + 10 if B > 5.
Now we count the number of "5"'s that appear the hundreds place. We note that if A > 5, then there are 100 such numbers (namely, 500-599). If A = 5, then there 10 * B + C + 1 such numbers (names, 500 - 5BC). If
A < 5 then there are none. Hence, the total number of "5"'s that appear in the hundreds place is 0 if A < 5, 10 * B + C + 1 if A = 5, and 100 if A > 5.
Therefore, the total number of 5's that appear in the numbers 1 through ABC can be summed as follows:
20 * A + B
+ 1 (if C > 5)
+ 1 (if B = 5)
+ 10 (if B > 5)
+ 10 * B + C + 1 (if A = 5)
+ 100 (if A > 5)
You can generalize this for any decimal digit i (where i = 1, 2, 3, 4, 5, 6, 7, 8 or 9) as follows:
20 * A + B
+ 1 (if C > i)
+ 1 (if B = i)
+ 10 (if B > i)
+ 10 * B + C + 1 (if A = i)
+ 100 (if A > i)
You can use the above "formula" to calculate the number of "5"'s that are used between a range of two 3-digit numbers (say, between 274 and 632) by simply calcuating how many are used from 1 through 632, and then subtracting how many are used from 1 through 274-1=273. The number of "5"'s used from 1 through 632 is 20 * 6 + 3 + 100, or 323. The number of "5"'s used from 1 through 273 is 20 * 2 + 7 + 10, or 57. Hence, there are 323-57=266 "5"'s used between 274 and 632.