Re: Avoid printing leading zero
- To: mathgroup at smc.vnet.net
- Subject: [mg100791] Re: Avoid printing leading zero
- From: Ray Koopman <koopman at sfu.ca>
- Date: Sun, 14 Jun 2009 05:39:07 -0400 (EDT)
- References: <h0sc2a$hi2$1@smc.vnet.net>
On Jun 11, 6:46 pm, Jos=E9 Lorenzana <lorenzana.j... at gmail.com> wrote:
> How do you avoid printing the leading zero in -0.03?
> I need to construct the string "-.03" .
>
> Something like this does not work:
> ToString at NumberForm[-0.3, {0, 2}]
I have thie following two routines in my init.m file.
'myform' suppresses exponential notation and substitutes a blank
space for the non-significant leading zero that precedes a fraction
in PaddedForm output: " 0.xyz" -> " .xyz", and "-0.xyz" -> " -.xyz".
The number of characters does not change. This is a quick-&-dirty
implementation. There are no checks for out-of-range values, special
cases, non-numbers, etc, so there may be error messages, more than
w+2 characters, or other anomalies.
myform[x__] := StringReplace[ToString@PaddedForm[x,
ExponentFunction->(Null&)],{" 0.0"->" .0", " 0.1"->" .1",
" 0.2"->" .2", " 0.3"->" .3", " 0.4"->" .4", " 0.5"->" .5",
" 0.6"->" .6", " 0.7"->" .7", " 0.8"->" .8", " 0.9"->" .9",
"-0.0"->" -.0", "-0.1"->" -.1", "-0.2"->" -.2", "-0.3"->" -.3",
"-0.4"->" -.4", "-0.5"->" -.5", "-0.6"->" -.6", "-0.7"->" -.7",
"-0.8"->" -.8", "-0.9"->" -.9"}];
'mform' is like 'myform', but in positive numbers the non-significant
zero is simply deleted, not replaced by a blank space, and the string
is one character shorter.
mform[x__] := StringReplace[ToString@PaddedForm[x,
ExponentFunction->(Null&)],{" 0.0"->" .0", " 0.1"->" .1",
" 0.2"->" .2", " 0.3"->" .3", " 0.4"->" .4", " 0.5"->" .5",
" 0.6"->" .6", " 0.7"->" .7", " 0.8"->" .8", " 0.9"->" .9",
"-0.0"->" -.0", "-0.1"->" -.1", "-0.2"->" -.2", "-0.3"->" -.3",
"-0.4"->" -.4", "-0.5"->" -.5", "-0.6"->" -.6", "-0.7"->" -.7",
"-0.8"->" -.8", "-0.9"->" -.9"}];