Go Down

### Topic: Manipulate a Number (Read 1 time)previous topic - next topic

#### ilovetoflyfpv

##### Feb 08, 2013, 05:37 am
Hi Gang

I was wondering how to manipulate a number?

Example

long serialNumber = 123456789

I am only interested in the last six digits. How can I extract these so that;

long serialNumber = 456789

Any help would be greatly appreciated.

Cheers

Jase

#### Delta_G

#1
##### Feb 08, 2013, 05:42 am
Use the % operator.  It gives you the remainder of a division.  If you % by 1000000 then the result will be the last six digits.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

#### JimboZA

#2
##### Feb 08, 2013, 05:47 am
Quote
Use the % operator.

Good thinking, Batman... I was starting to thing strings and right parts and stuff
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)

#### ilovetoflyfpv

#3
##### Feb 08, 2013, 07:04 am
Hi Guys

Thanks for the quick response. Can you give me an example?

Cheers

Jase

#### PaulS

#4
##### Feb 08, 2013, 07:23 am
Where are you getting this number? Why are you only interested in the last 6 digits? What if the number is only a 3 digit number?

#### WizenedEE

#5
##### Feb 08, 2013, 07:45 am
Code: [Select]
`long bignum = 2844493234;int smallernum = bignum % 1000000;`

#### JimboZA

#6
##### Feb 08, 2013, 11:05 am
Quote
Where are you getting this number? Why are you only interested in the last 6 digits? What if the number is only a 3 digit number?

Well the fact that the OP used the variable name serialNumber made me think it was exactly that, a serial number, and that they are of a specific length in his world. Often, the sections of serial numbers mean something: perhaps in his case the last 6 digits are the actual sequence number and that's what he needs to know for his purpose. Maybe the part before that is the factory and line number or something.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)

#### ilovetoflyfpv

#7
##### Feb 08, 2013, 08:45 pm
Hi Guys

Thanks for the replies. As mentioned earlier I'm interested in only the last six digits of the number which form a serial number. I think I understand now your suggestions. So by using % (modulo) it divides the number until it can't be divided any more then returns the remainder.

Example

x = 2844493234 % 1000000

x = 493234

Cheers

Jase

#### JimboZA

#8
##### Feb 09, 2013, 05:25 am
Quote
So by using % (modulo) it divides the number until it can't be divided any more then returns the remainder

Well no, it just divides it, not until it can't be divided any more, then returns the remainder.

Dividing by a power of ten is a kind of "special case" I guess one could call it, since it has the efect of moving the decimal place.

7%4 is 3, because 7/4 is 1.75, which is 1 remainder 3 (0.75 x 4=3)

123%10 is 3, because 123/10 is 12.3, which is 12 remainder 3 (0.3 x 10 = 3)
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)