Re: [Help?] Integer/Decimal portion of a real number
- To: mathgroup at smc.vnet.net
- Subject: [mg8866] Re: [mg8847] [Help?] Integer/Decimal portion of a real number
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Tue, 30 Sep 1997 20:16:30 -0400
- Sender: owner-wri-mathgroup at wolfram.com
kammy shan leung <ksleung at students.uiuc.edu>
[mg8847] [Help?] Integer/Decimal portion of a real number
writes
> What I would like to do is break up a real number to
> its integer and decimal portion.
> For example:
>
> 4.12345 can be broken up to:
>
> integer_portion = 4
> decimal_portion = 12345
Kammy,
We have (slightly modifying your example)
inpt= IntegerPart[4.0012345]
4
frpt=FractionalPart[4.0012345 ]
0.0012345
But if you want to get 0012345 from the last output then one
problem is to get the digits, which can be done) and another one is
that 0012345 will be reduced to 12345 (which can be tackled by
storing a list of digits or a string). For example
StringDrop[ToString[frpt],2]
"0012345"
Strings can be converted by using the functions Characters and
ToExpression.
If strings are acceptable then we can move into strings right at
the beginning and get a function
infr[x_Real]:=
(str = ToString[x];
dp = StringPosition[str,"."][[1,1]];
{StringTake[str,dp-1],StringDrop[str,dp]}
)
Which gives
infr[4.0012345]
{"4","0012345"}
Note: The function RealDigits stores the information about frpt
that we want, but problems associated with approximatig inexact
numbers show up:
RealDigits[frpt]
{{1,2,3,4,4,9,9,9,9,9,9,9,9,8,0,5},-2}
Allan Hayes
hay at haystack.demon.co.uk
http://www.haystack.demon.co.uk/training.html
voice:+44 (0)116 2714198
fax: +44 (0)116 2718642
Leicester, UK