Re: Simplest way to get 2 digits from an integer...
- To: mathgroup at smc.vnet.net
- Subject: [mg110937] Re: Simplest way to get 2 digits from an integer...
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Mon, 12 Jul 2010 07:22:01 -0400 (EDT)
On 7/12/10 at 1:04 AM, rodrigomurtax at 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@StringTake[ToString[a], 2] >12 >2) a = 1234; >FromDigits@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.