From: Murta on
Hello All

I would like to find a simplest code to take the first n digits os a integer.
Here are two examples (not simple) to take the first 2 digits os the number a:

1)
a = 1234;
ToExpression(a)StringTake[ToString[a], 2]
12

2)
a = 1234;
FromDigits(a)Take[IntegerDigits[a],2]
12
-------
In Excel, I just use Left(a,2)*1 (multiply by 1 to get a number,
instead of text)
There is one simplest way in Mathematica!?

thanks in advance
Rodrigo Murta

From: Peter Breitfeld on
Murta wrote:

> Hello All
>
> I would like to find a simplest code to take the first n digits os a integer.
> Here are two examples (not simple) to take the first 2 digits os the number a:
>
> 1)
> a = 1234;
> ToExpression(a)StringTake[ToString[a], 2]
> 12
>
> 2)
> a = 1234;
> FromDigits(a)Take[IntegerDigits[a],2]
> 12
> -------
> In Excel, I just use Left(a,2)*1 (multiply by 1 to get a number,
> instead of text)
> There is one simplest way in Mathematica!?
>
> thanks in advance
> Rodrigo Murta
>

You can use IntegerDigits and FromDigits like this

firstN[a_Integer, n_Integer]:=FromDigits(a)IntegerDigits[a][[1;;n]]

In= firstN[1234,2]
Out=12

--
_________________________________________________________________
Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de

From: Bill Rowe on
On 7/12/10 at 1:04 AM, rodrigomurtax(a)gmail.com (Murta) wrote:

>Hello All
>
>I would like to find a simplest code to take the first n digits os a
>integer. Here are two examples (not simple) to take the first 2
>digits os the number a:

>1) a = 1234; ToExpression(a)StringTake[ToString[a], 2]
>12

>2) a = 1234;
>FromDigits(a)Take[IntegerDigits[a],2]
>12

Simplest is somewhat subjective. Here are two other ways:

In[2]:= Floor[10^(1 + FractionalPart[Log[10, a]])]

Out[2]= 12

In[3]:= FromDigits[RealDigits[a] /. {a_, _} :> {a[[;; 2]], 2}]

Out[3]= 12

This last will return the first two significant digits of any
real number regardless of where the decimal point is.


From: Murray Eisenberg on
You say you want the first two digits, but actually the results you're
showing produce an integer having those two digits. Here's a way for
both, without resorting to strings at all:

n = 49804713;
Take[IntegerDigits[n], 2]
{4, 9}

FromDigits[Take[IntegerDigits[n], 2]]
49


On 7/12/2010 1:04 AM, Murta wrote:
> Hello All
>
> I would like to find a simplest code to take the first n digits os a integer.
> Here are two examples (not simple) to take the first 2 digits os the number a:
>
> 1)
> a = 1234;
> ToExpression(a)StringTake[ToString[a], 2]
> 12
>
> 2)
> a = 1234;
> FromDigits(a)Take[IntegerDigits[a],2]
> 12
> -------
> In Excel, I just use Left(a,2)*1 (multiply by 1 to get a number,
> instead of text)
> There is one simplest way in Mathematica!?
>
> thanks in advance
> Rodrigo Murta
>

--
Murray Eisenberg murray(a)math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305

From: Matthias Bode on
Hola:

n2 == 95231;
IntegerPart[n2/10^IntegerPart[Log[10, n2] - 1]]

Simple(st)?

Best regards,

MATTHIAS BODE
COCHABAMBA/BOLIVIA

> Date: Mon, 12 Jul 2010 07:22:22 -0400
> From: phbrf(a)t-online.de
> Subject: Re: Simplest way to get 2 digits from an integer...
> To: mathgroup(a)smc.vnet.net
>
> Murta wrote:
>
> > Hello All
> >
> > I would like to find a simplest code to take the first n digits os a in=
teger.
> > Here are two examples (not simple) to take the first 2 digits os the nu=
mber a:
> >
> > 1)
> > a == 1234;
> > ToExpression(a)StringTake[ToString[a], 2]
> > 12
> >
> > 2)
> > a == 1234;
> > FromDigits(a)Take[IntegerDigits[a],2]
> > 12
> > -------
> > In Excel, I just use Left(a,2)*1 (multiply by 1 to get a number,
> > instead of text)
> > There is one simplest way in Mathematica!?
> >
> > thanks in advance
> > Rodrigo Murta
> >
>
> You can use IntegerDigits and FromDigits like this
>
> firstN[a_Integer, n_Integer]:==FromDigits(a)IntegerDigits[a][[1;;n]]
>
> In== firstN[1234,2]
> Out==12
>
> --
> _________________________________________________________________
> Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de
>