One gram is exactly 1,000 milligrams. It is a plain decimal shift, but it is the conversion where a slipped decimal point does the most damage, because it sits at the centre of how medicine doses are written.
About the units
Both units are the gram with or without a prefix: milli means a thousandth, from the Latin mille. The gram itself was originally defined as the mass of one cubic centimetre of water at the melting point of ice, and although the modern definition runs through the kilogram and the Planck constant, that origin is why a millilitre of water still weighs close to a gram and a microlitre close to a milligram. The milligram is the working unit of pharmacology, toxicology and nutrition because typical active doses fall naturally in the range of one to a thousand of them.
The exact factor
The factor is exactly 1,000: shift the decimal three places. 0.5 g is 500 mg; 25 mg is 0.025 g. The next step down is the microgram, a thousandth of a milligram, and that is where the real danger lies. Writing a dose as 0.5 mg rather than 500 µg, or vice versa, is a documented and repeated cause of medication error, which is why clinical guidance discourages leading decimals and abbreviations: 500 micrograms written out in full cannot be misread the way ".5 mg" can.
Where you meet this conversion
Medication is the dominant case. Tablet strengths are printed in milligrams, dosing guidance is calculated per kilogram of body weight, and the two ends of that chain sit six orders of magnitude apart. Nutrition labels use grams for macronutrients and milligrams for sodium, calcium and most vitamins, so the same panel carries both. Analytical chemistry, jewellery and reloading ammunition all work in milligrams for the same reason: it is the smallest unit an ordinary balance can resolve reliably.