FNUMBER Function

Material prepared from M Programming Book [WALTERS1997]

Page 81.

$FNUMBER

The $FNUMBER or $FN function returns a formatted string containing a number that was passed as argument. For example:

GTM> W $FNUMBER(-100,"-");
100

The formatting is specified using the second argument the function takes. It takes a string with zero or more special character, which are interpreted as follows:

  • “+”: puts a “+” in front of positive values.
  • “-”: removes the “-” from negative values.
  • ”,”: puts commas in every third position left of the decimal point.
  • “T”: represents the number with a trailing sign.
  • “P”: puts negative numbers between parenthesis.

This way, the function can be used as below:

GTM> S A=-100 S B=30000
GTM> W $FN(B,"+")
+30000
GTM> W $FN(A,"-")
100
GTM> W $FN(B,",')
30,000
GTM> W $FN(A,"T")
100-
GTM> W $FN(A,"P")
(100)

Also, characters can be combined together in order to get specific formatting. For example:

GTM> S A=-100 S B=30000
GTM> W $FN(A,"+T")
100-
GTM> W $FN(B,"+T")
30000+

Finally, a third argument can also be specified in order to define the number of digits after the decimal poing. For example:

GTM> W $FN(1/3,"",3)
0.333