MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Formatted output

  • To: mathgroup at
  • Subject: [mg39258] Re: [mg39244] Formatted output
  • From: Tomas Garza <tgarza01 at>
  • Date: Thu, 6 Feb 2003 03:08:03 -0500 (EST)
  • References: <>
  • Sender: owner-wri-mathgroup at

Actually, you may format your output to any extent of embellishment 
desired. Suppose, e.g., you have the following table as the result of 
your calculations:

result = Table[Random[Real, {0, 10000}], {10}, {7}];

The display (omitted here) shows a variety of numbers, with one, two, 
three or four integers, and ditto for decimals. You might simply take


and obtain a table with constant-width columns and numbers 
right-aligned. However, this is not too nice, since numbers with one 
integer take the same space as numbers with four integers. So you might 
want to go much further and format your numbers so that, for example, 
only two decimals are shown, and integral parts are separated with 
commas in groups of three, say. To start with, then, define a function, 
say imp, which will preserve only two decimals and perform the grouping, 
separating with commas in groups of three:


imp[x_] := NumberForm[x, {12, 2}, NumberPadding -> {"", "0"}, 
DigitBlock -> 3,
    ExponentFunction -> (Null & )];

and apply it to your result

c = Map[imp, result, {2}] 

Then the following will produce a very nice table

DisplayForm[StyleBox[GridBox[c, GridFrame -> 2, RowLines -> True, 
ColumnLines -> True,
     ColumnAlignments -> {Right}]]]

You may change the font, add column headings or row titles, or whatever 
other features you wish.

Of course, you may have to spend a few hours understanding what is going 
on (as I had to do at one time or another), but I think it will be worth 
your effort.

Tomas Garza
Mexico City
----- Original Message -----
From: "Steve Gray" <stevebg at>
To: mathgroup at
Subject: [mg39258] [mg39244] Formatted output

> Hello all,
> I want to produce a table of text output (in the same notebook
> my code is in). The output consists of many rows of numbers, each row
> containing the same number of numbers. For example the entire output
> might consist of 500 rows each having 7 columns. It is important for
> understanding the output to have well-formatted columns of a constant
> width regardless of the number of digits in each number (which will be
> no more than say 9). In the columns the numbers should be
> right-aligned.
> "Print" does not seem to have enough control to do this. It's
> much less flexible than the print statement in C. What I need is
> something like that. I've been reading the book, sections 2.7, 2.8,
> etc., and it looks like I will have to write an actual (small) program
> to do this.
> I'd appreciate any suggestions or leads to a package where all
> this is already done.
> Many thanks.
> Steve Gray

  • Prev by Date: Re: Formatted output
  • Next by Date: Random Numbers
  • Previous by thread: Formatted output
  • Next by thread: Re: Formatted output