Motivated by the work of [K. Girstmair, A “popular” class number formula, Amer. Math. Monthly 101(10) (1994) 997–1001; K. Girstmair, The digits of [Formula: see text] in connection with class number factors, Acta Arith. 67(4) (1994) 381–386] and [M. R. Murty and R. Thangadurai, The class number of [Formula: see text] and digits of [Formula: see text], Proc. Amer. Math. Soc. 139(4) (2010) 1277–1289], we study the average of the digits of the [Formula: see text]-adic expansion of [Formula: see text] whenever [Formula: see text] is a product of two distinct primes or a prime power. More explicitly, if [Formula: see text] is an integer such that [Formula: see text] and suppose that [Formula: see text] is the [Formula: see text]-adic expansion of [Formula: see text] then we establish the average of the digits of the [Formula: see text]-adic expansion of [Formula: see text] in terms of [Formula: see text] and the “trace” of generalized Bernoulli numbers [Formula: see text] where [Formula: see text]’s are odd Dirichlet characters modulo [Formula: see text] As a consequence of these results, we recover two well-known results of Gauss and Heilbronn (see Theorems 1.6 and 1.7).