Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Simplest way to get 2 digits from an integer...

  • To: mathgroup at smc.vnet.net
  • Subject: [mg110946] Re: Simplest way to get 2 digits from an integer...
  • From: Murray Eisenberg <murray at math.umass.edu>
  • Date: Tue, 13 Jul 2010 05:26:59 -0400 (EDT)

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@StringTake[ToString[a], 2]
> 12
>
> 2)
> a = 1234;
> FromDigits@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 at 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


  • Prev by Date: Re: Stem and Leaf Question
  • Next by Date: Re: Simplest way to get 2 digits from an integer...
  • Previous by thread: Re: Simplest way to get 2 digits from an integer...
  • Next by thread: Re: Simplest way to get 2 digits from an integer...