[Date Index]
[Thread Index]
[Author Index]
Re: How can I transform the number of 123.456 into "123.456"?
*To*: mathgroup at smc.vnet.net
*Subject*: [mg26527] Re: [mg26515] How can I transform the number of 123.456 into "123.456"?
*From*: Tomas Garza <tgarza01 at prodigy.net.mx>
*Date*: Wed, 3 Jan 2001 04:08:45 -0500 (EST)
*References*: <200101010732.CAA09656@smc.vnet.net>
*Sender*: owner-wri-mathgroup at wolfram.com
I see your problem. If you have a real number, a simple ToString[] will not
work the way you want, since there will be a roundoff at the end:
In[1]:=
ToString[345.89087]
Out[1]=
"345.891"
Then I suggest the following approach. Take first
In[2]:=
a = RealDigits[345.89087]
Out[2]=
{{3, 4, 5, 8, 9, 0, 8, 7, 0, 0, 0, 0, 0, 0, 0, 0}, 3}
Notice that the last element gives the number of digits that are to the left
of the decimal point, 3 in this example. Then search for the significant
digits, i.e. get rid of the sequence of zeros (if any) at the end. Define a
function zeroAtTheEnd:
In[3]:=
zeroAtTheEnd[y_] := y /. {x__, 0} -> {x}
which gets rid of the ending zero, if any. Nest this function until the
result no longer changes, i.e., there is no zero at the end. To do this, we
use FixedPoint[]:
In[4]:=
b = FixedPoint[zeroAtTheEnd, a[[1]]]
Out[4]=
{3, 4, 5, 8, 9, 0, 8, 7}
Finally,
In[5]:=
ToString[FromDigits[Take[b, a[[2]]]]] <> "." <>
ToString[FromDigits[Take[b, -Length[b] + a[[2]]]]]
Out[5]=
"345.89087"
**However**, this approach will not work always, since Mathematica has an
odd way to handle approximate numbers. For example,
In[6]:=
RealDigits[345.89087]
Out[6]=
{{3, 4, 5, 8, 9, 0, 8, 7, 0, 0, 0, 0, 0, 0, 0, 0}, 3}
but
In[7]:=
RealDigits[89087.345]
Out[7]=
{{8, 9, 0, 8, 7, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 1}, 5}
and now you get an unexpected (and undesirable) 1 at the end of the sequence
of digits! It seems that a general approach would have to be of a less
elegant nature, viz, if you have an integer number, then just use
ToString[]; otherwise, multiply the number by 10 to the power "number of
figures to the right of the decimal point", i..e. make it an integer first,
and then proceed as indicated above, but you must count visually the number
of significant figures in your number. Example:
In[8]:=
c = FixedPoint[zeroAtTheEnd, RealDigits[89087.345*10^3][[1]]]
Out[8]=
{8, 9, 0, 8, 7, 3, 4, 5}
In[9]:=
ToString[FromDigits[Take[c, 5]]] <> "." <> ToString[FromDigits[Take[c, -3]]]
Out[9]=
"89087.345"
There is probably a much simpler way to do what you want, but this is what
came to my mind first.
Tomas Garza
Mexico City
----- Original Message -----
From: "liwen liwen" <gzgear at yahoo.com>
To: mathgroup at smc.vnet.net
Subject: [mg26527] [mg26515] How can I transform the number of 123.456 into "123.456"?
> Dear friend:
> Happy New Year!
> I want to transform the numbers such as 123.456 or
> 345.89087 into strings as "123.456" or "345.89087" ,
> Could you help me?
> Thank you very much!
>
> Best regards,
>
> Liwen
> E-mail: gzgear at yahoo.com
>
> 12/30/2000
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Photos - Share your holiday photos online!
> http://photos.yahoo.com/
>
Prev by Date:
**Re: Re: plot time vs date**
Next by Date:
**mathematica log sessions**
Previous by thread:
**Re: How can I transform the number of 123.456 into "123.456"?**
Next by thread:
**Re: How can I transform the number of 123.456 into "123.456"?**
| |