The relative formula mass is calculated by adding up all the different atomic masses of the atoms present. The mass needs to be multiplied by the number of atoms present respectively. For instance, as there are 2 oxygen atoms present, it means that we need to multiply 16 by 2 to get 32. Hydrogen has a mass of 1, so 1 multiply by 2 is equal to 2.
There is only one atom of magnesium; hence, we will add the relative atomic mass of the atom, which is 24. Now adding 24, 32 and 2 gives us the value 58. Hence, the relative molecular mass of Mg(OH)2 is 58g.