[Jprogramming] Formatting output in J

Chris Burke cburke at jsoftware.com
Thu Jan 3 11:16:33 HKT 2008


  McGuinness, Brian wrote:
 > ...
> I see no equivalent functionality provided by 8!:n.  The 8!:n formatter 
> would  be much more useful if the Z, G, and <...> text insertion from #FMT were 
> added  to it.
> ...

These are indeed facilities in #FMT that are not yet supported in J, 
thanks for pointing them out. You will have to preprocess the data or 
postprocess the result to get what you want. If your data is numeric, it 
is probably easiest to postprocess the result. Use 8!:0 or 8!:1 to get a 
boxed result, then adjust each box as appropriate and join them up.

For example, the following shows how you might generate the equivalent 
of G and Z formats. Text inserts can also be done this way.

fmttel=: 'p< (>q<) >7.0,3.0,p<->5.0' (8!:2) 0 1000 10000 #: ]

fmtsig=: 3 : 0
dat=. 'r<0>8.2' 8!:2 ,.|y
' ',.((y<0){'0-') 0}"0 1 dat
)

myfmt=: 3 : 0
dat=. '2.0,,,4.0' 8!:1 y
dat=. (<fmttel 1{"1 y) 1} dat
dat=. (<fmtsig 2{"1 y) 2} dat
 >,.&.>/dat
)

    [M=: (20 1234567890+/~i.3),.(200*2 o.1 2 3),.165 175 190
20 1.23457e9   108.06 165
21 1.23457e9 _83.2294 175
22 1.23457e9 _197.998 190

    myfmt M
20 (123) 456-7890 00108.06 165
21 (123) 456-7891 -0083.23 175
22 (123) 456-7892 -0198.00 190



More information about the Programming mailing list