Solve the ideal gas law for any variable.
The ideal gas law PV = nRT ties the four state variables together, so knowing three fixes the fourth. Solving for the amount of substance gives n = PV/RT, and from there the molecule count follows through Avogadro's number and the density through the molar mass. The STP volume is included because it is the natural yardstick: one mole of ideal gas occupies 22.414 litres at 0 C and 1 atm.
Ideal gas law solved for moles
n = P V / (R T) with R = 0.08206 L atm / (mol K); density = n x molar mass / V; molecules = n x 6.022e23
It depends on your units. This calculator uses 0.08206 L atm/(mol K) to match atmospheres and litres. With pascals and cubic metres use 8.314 J/(mol K); the two describe the same constant.
Because the same mass of gas occupies different volumes. Density is mass over volume, and since n and therefore mass rise with pressure and fall with temperature at fixed volume, gas density is not a fixed property the way a liquid's is.
Under a few atmospheres and well above the boiling point, typically within 1-2 percent. Errors grow to tens of percent near condensation or above roughly 10 atmospheres, where van der Waals or a more elaborate equation of state is needed.