Often I see mm stuck with the number like this: 9mm semi auto pistol. However, in most situations we see "10 km", so are there specific rules in certain situations? Because it seems for gun the unit of measurement is stuck with the number, but in most other cases we separate them with a space. Also, is there a software that helps you format sentences? I found one for capitalization, but I am wondering if there's something for measurement units and spaces.
For formatting S.I. units the standards are posted by the National Institute of Standards and Technology link.
To conform to the standard there should be a space between the numerical value of the quantity and the unit: "9 mm". The usual "9mm" is a non-standard format that has become accepted in common usage (for better or worse).
If you are doing technical documents that require properly formatted units, you might have to go to the extreme by using a system like LaTeX. (In which case there is the siunitx package to achieve just that.)