Hi z_lex,
A 'custom format' only formats how a number is displayed.
It does not convert the number to an equivalent value using another unit.
Column A shows the raw numbers representing the number of grams mass of a variety of samples.
Column B shows the same numbers formatted using the custom format suggested above.
B4 shows the mass of approximately half a cup (8 tablespoons) of water.
B5 shows a mass between that of a Smart ForTwo car and a 2018 Mini Cooper.
Numbers doesn't support a formula that both converts and formats a metric value to appropriate units. In is possible to use a formula to differentiate between numbers containing a fractional part (not .00 though) and those with no decimal part, then append the appropriate symbol. The result, though is a text value, which may not be compatible with math to be down with that value.
=D2&IF(D2=INT(D2),"g","kg")
The formula shown is in E2, and filled down to E9. Text alignment has been set to 'align right.'
Regards,
Barry