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: [mg110950] Re: Simplest way to get 2 digits from an integer...
  • From: Matthias Bode <lvsaba at hotmail.com>
  • Date: Tue, 13 Jul 2010 05:27:42 -0400 (EDT)

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 at t-online.de
> Subject: [mg110939] Re: Simplest way to get 2 digits from an integer...
> To: mathgroup at 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@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
> >
>
> You can use IntegerDigits and FromDigits like this
>
> firstN[a_Integer, n_Integer]:==FromDigits@IntegerDigits[a][[1;;n]]
>
> In== firstN[1234,2]
> Out==12
>
> --
> _________________________________________________________________
> Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de
>


  • Prev by Date: Re: Simplest way to get 2 digits from an integer...
  • Next by Date: Re: Why Evaluate[Defer[1+1]] does not return 2 ?
  • 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...