Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Printing numbers

  • To: mathgroup at smc.vnet.net
  • Subject: [mg54536] Re: [mg54483] Printing numbers
  • From: DrBob <drbob at bigfoot.com>
  • Date: Tue, 22 Feb 2005 04:24:20 -0500 (EST)
  • References: <200502210844.DAA27227@smc.vnet.net>
  • Reply-to: drbob at bigfoot.com
  • Sender: owner-wri-mathgroup at wolfram.com

There's no reason to think ToString[123.456789] should have ten characters. In fact, it has seven:

ToString[123.456789]

123.457

That's because the default display for an approximate (machine precision) number is six decimal places. It could have been 1 or 100, after all. By the time the number is given to ToString, Mathematica has stored the number AS a number and forgotten what your InputForm was.

Similarly, ToString turns 1234567.89 into a two-line output taking up 22 characters:

ToString[1.23456789*^6]
"          6\n1.23457 10"

To control display of numbers, experiment with NumberForm, PaddedForm, AccountingForm, etc.

I doubt you'll be very happy with it.

Bobby

On Mon, 21 Feb 2005 03:44:51 -0500 (EST), Steve Gray <stevebg at adelphia.net> wrote:

> 1. Can anyone tell me why these results are what they are? Neither answer is what I would expect,
> which is 10. The two strings are the same length.
>
> Print[StringLength[ToString[123.456789]], "   ",
>         StringLength[ToString[1234567.89]]];
> 7    22
>
> 2. Is there a simple, easily available or self-evident way to get numbers printed out such that the
> total  number of spaces occupied by the print before the decimal point is some fixed, user-specified
> amount, the number of spaces occupied after the decimal point is a different user-specified amount,
> and the number of spaces following the number is a third user-specified amount. The goal is to
> easily produce aligned columns of numerical printing, which would seem to be the most trivial task
> in neat number printing.
> 	For example if the function is called fprint, fprint[345.678,4,5,6] would print 4 spaces
> followed by 5 character positions to the left of the point (00345 or (2 spaces)" 345", specified by
> another argument), and 6 to its right. So fprint[345.678,4,5,6] would print (4 spaces)(2
> spaces)"345.678"(3 spaces), or if desired (4 spaces)(2 spaces)"345.678000"(no spaces), the total
> taking up 16 positions. Also fprint[-345.678,4,5,6] would print (4 spaces)(1 space)"-345.678"(3
> spaces), also taking 16 positions, with the decimal points aligned with the first example.
> 	Help is not that helpful, implying that I have to write one or find it somewhere. As I
> recall, C provides an easy way to do this by setting arguments to Print.
> 	Leads would be appreciated.
>
> Steve Gray
>
>
>
>
>



-- 
DrBob at bigfoot.com
www.eclecticdreams.net


  • Prev by Date: Re: Re: my mathematica no longer renders graphics, even after reinstall
  • Next by Date: Re: Printing numbers
  • Previous by thread: Printing numbers
  • Next by thread: Re: Printing numbers