Re: ToString Awkwardness
- To: mathgroup at smc.vnet.net
- Subject: [mg126872] Re: ToString Awkwardness
- From: James Stein <mathgroup at stein.org>
- Date: Thu, 14 Jun 2012 05:34:01 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201206130855.EAA03690@smc.vnet.net>
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 at 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 > >
- References:
- ToString Awkwardness
- From: "Kevin J. McCann" <kjm@KevinMcCann.com>
- ToString Awkwardness