Internal WRITEs are fast. In the Fortran-66 days, WRITEs were slow, but that wasn’t a code issue – it was due to the mechanical slowness of first line printers (or card or tape punches), then magnetic tapes.
You don’t need a rigidly fixed format like F6.2, because in an internal write the format can be contained in a character variable, e.g. if you know n and m (in Fn.m) then
WRITE (STRING1,’’’(F’,I3,’.’,I3,’)’’’ ) n, m
WRITE (STRING2, STRING1) x
(Apologies if I haven't got the right number of apostrophes. I usually use both single and double quotes - permissible since Fortran 90 - for readability, and several trial before getting it right).
In order to avoid lots of IFs, you can use the LOG10 function – although this only works for positive X, so
N1 = LOG10 ( ABS (X) )
where N1 is the number of characters before the decimal point for a number >1.0.
You may need to add 1 for a negative sign (and if you want a + plus sign). N1 is at least 1 more even for numbers <1.0 if you want a leading zero.
To get n, you need to consider the desired number of decimal places, plus N1 + 1, where the extra 1 if for the decimal point.
(It is possible to write X with a big n, then remove leading blank characters, but I prefer LOG10).
There is a maximum meaningful n, due to the number of significant contained decimal digits of precision (7 in REAL*4 ?). This used to be a big problem with all the different formats in mainframes (16, 24, 30, 32, 36 and 60 bit words in my personal experience, plus all the complexities of DOUBLE PRECISION), but now all we need to consider are 32, 64 and 80 bit precisions (and not even the latter in .NET). There are also practical limits on what values of n and m are sensible before scientific notation is anyway a more sensible option (for an example, see what scientific calculators do!).
A general purpose floating point variant of CNUM would also need to error-handle undefined numbers and special cases like NaN.
Most applications have intrinsic limits on range and precision for variables that contain physical values. For example, no distance on the planet earth can be more than 40 000 000 metres (plus a tiny bit!) or be negative, hence 2 < =N1 < = 8. Moreover, if a millimetre accuracy is adequate, then we can probably fix m=3, and n therefore between 5 and 12. I am sure that other forum users have their own examples. It probably means that even though n and m can in principle be calculated, the programmer probably needs to have some control over what is actually selected, even if they do not need to choose,* a priori*, what n and m are.
CNUM is a lot easier than the floating point equivalent because the range for integers is small, and the precision absolute.