Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2002

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

Search the Archive

RE: TableForm and NumberForm

  • To: mathgroup at
  • Subject: [mg34746] RE: TableForm and NumberForm
  • From: "DrBob" <majort at>
  • Date: Tue, 4 Jun 2002 03:42:05 -0400 (EDT)
  • Reply-to: <drbob at>
  • Sender: owner-wri-mathgroup at

Here's another manifestation of the problem (notice a semicolon in the
fifth line but not the second):

NumberForm[tb2, {12, 11}];
% // FullForm
NumberForm[tb2, {12, 11}];
% // FullForm

It is display of NumberForm objects (not the use of TableForm) that
sometimes removes the NumberForm head.  It only removes an outermost
NumberForm head, not NumberForm heads embedded within another Head.

Isn't that the pattern we're seeing?

In a construct like TableForm[NumberForm...]...] we want to remove
NumberForm heads that enclose lists but not when they enclose numbers.
And, we want to propagate those NumberForm heads downward TO the numbers
rather than simply removing them, so we have this option:

numRule := NumberForm[a_List, b_] :> (NumberForm[#, b] & /@ a)
TableForm[NumberForm[tb2, {12, 11}] //. numRule]

This is no easier than mapping NumberForm onto individual array elements
(that IS what we're doing), but it postpones that to the last moment, in
case that's useful.  However, it would be tedious to format different
columns differently with a variation of this.  For that, I gave a better
solution earlier:

formRule := {a_Real, b_Real, c_Real, d_Real} :> {
      NumberForm[a, {15, 14}],
      NumberForm[b, {3, 2}],
      ScientificForm[c, {3, 2}],
      AccountingForm[d, 8]}
TableForm[tb2 /. formRule, TableHeadings -> {None, {"a", "b", "c",

Use of numRule is simpler if all numbers in the table need the same
formatting.  In more complicated settings, both techniques could be used
at once.

The key (am I right?) is that TableForm doesn't remove NumberForm heads
at all, and displaying an object only removes the outermost NumberForm

Bobby Treat

-----Original Message-----
From: David Park [mailto:djmp at] 
To: mathgroup at
Subject: [mg34746] TableForm and NumberForm

I've renamed this thread because it is a separate subject, probably
interesting to a number of people.

What is the interaction between NumberForm and TableForm? It seems like
butted my head against this problem a number of times.

In the following, NumberForm produces an array with the desired number
format. If you look at FullForm, the whole array is wrapped in
But when the TableForm statement is evaluated, the NumberForm is thrown
away. It is as if TableForm was Applied. Or else NumberForm is stripped
away. We revert to the default 6 place display.

tbl = Table[Random[], {3}, {4}];
NumberForm[tbl, {12, 11}]

In the following NumberForm is mapped onto the individual array
The array displays with the desired number format. Now, when we use
TableForm, the number format is preserved and we obtain the desired
So, why is NumberForm stripped away in the first case and not in this

tb2 = Table[Random[], {3}, {4}];
Map[NumberForm[#, {12, 11}] &, %, {-1}]

If we map NumberForm onto each row in the array, then TableForm acts as
it had a list of three elements, each element with the Head NumberForm.
displays as a column of three lists.

tb3 = Table[Random[], {3}, {4}];
Map[NumberForm[#, {12, 11}] &, %, {1}]

So why didn't the first case act as a TableForm of a single element? If
rewrite the first case this way, it does. We obtain a single element in
table, namely the entire array wrapped in NumberForm.

tb4 = Table[Random[], {3}, {4}];
TableForm[NumberForm[tb4, {12, 11}]]

In any case, it appears that when using TableForm and NumberForm, the
procedure is to map NumberForm onto the individual array elements.

David Park
djmp at

> From: DrBob [mailto:majort at]
To: mathgroup at
> Bob,
> Thanks!  My first attempt was evidently too simplistic.  But look at
> difference between these two output forms:
> tbl = Table[Random[], {3}, {4}]
> NumberForm[TableForm[tbl], {12, 11}]
> TableForm[NumberForm[tbl, {12, 11}]]
> Is that what you'd expect?  (I'm seeing brackets, etc. displayed in
> second version.)
> Bobby
> From: BobHanlon at [mailto:BobHanlon at]
To: mathgroup at
> In a message dated 6/3/02 3:12:04 AM, majort at writes:
> >ODDLY ENOUGH, when I tried to use NumberForm within TableForm, the
> >result didn't display as a table, so I don't know how to show
> >columns of the table at different display precisions.
> >
> >QUESTION: Does anybody know how to do that?
> TableForm[
>   Map[
>     NumberForm[#, {4, 3},
>         NumberPadding->{"", "0"}]&,
>     Table[Random[], {3}, {4}], {2}],
>   TableHeadings->{Automatic, Automatic}]
> TableForm[
>   Table[
>     NumberForm[
>       Random[], {5, n},
>       NumberPadding->{"", "0"}],
>     {3}, {n, 4}],
>   TableHeadings->{Automatic, Automatic}]
> Bob Hanlon
> Chantilly, VA  USA

  • Prev by Date: TableForm and NumberForm
  • Next by Date: RE: RE: RE: Define a function
  • Previous by thread: TableForm and NumberForm
  • Next by thread: Re: TableForm and NumberForm