From: Murta on 13 Jul 2010 05:26 On Jul 12, 8:22 am, Peter Breitfeld <ph...(a)t-online.de> wrote: > 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 Thanks for all the anwserns I was subjective saing simple. I was lookinng for a way to use just one function like excel. Here we use 3 functions. But thanks for the other ways!
From: Murray Eisenberg on 14 Jul 2010 05:36 To the contrary, in Excel you are using TWO functions: Left and * . On 7/13/2010 5:26 AM, Murta wrote: > On Jul 12, 8:22 am, Peter Breitfeld<ph...(a)t-online.de> wrote: >> 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 > > Thanks for all the anwserns > I was subjective saing simple. I was lookinng for a way to use just > one function like excel. Here we use 3 functions. > But thanks for the other ways! > -- 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: Raffy on 15 Jul 2010 03:09
On Jul 11, 10:04 pm, Murta <rodrigomur...(a)gmail.com> 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 ClearAll[takeDig]; takeDig[n_Integer, c_Integer, b_Integer:10] := Which[ n == 0 || c == 0, 0, n < 0, -takeDig[-n, c, b], c > 0, Quotient[n, b^Ceiling[Log[b, n + 1] - c]], c < 0, Mod[n, b^-c] ]; takeDig[12345,3] === 123 takeDig[12345,-3] === 345 takeDig[-12345,3] === -123 takeDig[-12345,-3] === -345 takeDig[10000, 1] === 1 takeDig[10000, -4] === 0 takeDig[0, _] === 0 takeDig[_, 0] === 0 Or, if you just want the n > and c > 0 case: takeDig[n_, c_] := Quotient[n, 10^Ceiling[Log10[n + 1] - c]]; |