Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #10463

Re: ToString Awkwardness

From James Stein <mathgroup@stein.org>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: ToString Awkwardness
Date 2012-06-14 09:36 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jrcbbf$dtv$1@smc.vnet.net> (permalink)
References <201206130855.EAA03690@smc.vnet.net>

Show all headers | View raw


Here are two routines I wrote long ago.
Perhaps they'll be a bit of help...

jhsForm::usage="jhsForm[r,n] -> returns a string to display r with n digits
after the decimal point. If n is larger than the accuracy of the number, n
will be truncated. If n <= 0, 0 will be used.

jhsForm[r,n,w,padChar] ->is as above, except the string will be a least w
characers in width, prepadded with padChar if necessary.";

jhsForm[r_, fD_Integer: - 1] := Module[{nd = 999, fd},
   (* nd,fd are arguments to 'NumberForm' *)
   (*
   fd := # places to right of decimal point *)
   If[Abs[r] == Infinity,
    Return[If[Sign[r] > 0, "=E2=88=9E", "-=E2=88=9E"]]];
   If[fD >= 0,
    fd = fD,
    fd = Max[Min[Accuracy[r], 2]];
    ];
   If[fd == 0,(* elide decimal point: *)
    Return[ToString[Round[r]]],
    Return[ToString[NumberForm[r // N, {nd, fd}]]];
    ];
   ];
jhsForm[r_, fD_: - 1, fW_: 0, pad_: " "] := Module[{s},
   s = jhsForm[r, fD];
   While[StringLength[s] < fW, s = pad <> s];
   s
   ];



On Wed, Jun 13, 2012 at 1:55 AM, Kevin J. McCann <kjm@kevinmccann.com>wrote:

> I have several plots with annotation that includes parameter values. I
> am using ToString inside a Text command to do this, and all is well
> unless the values are small so that the results are in scientific
> notation. When this happens, I get something like this:
>
> "f(x) = " <> ToString[8.71 10^-15]
>
> f(x) =        -15
>        8.71 10
>
> Which is a bit awkward looking in the plot. Upon further investigation,
> I looked at the FullForm of the above and got:
>
> "f(x) =        -15\n8.71 10"
>
> So, I see why the output "looks funny". The exponent is treated
> separately from the 10. Any suggestions for nicer looking output?
>
> Thanks,
>
> Kevin
>
>

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: ToString Awkwardness James Stein <mathgroup@stein.org> - 2012-06-14 09:36 +0000

csiph-web