Pages: [1]   Go Down
Author Topic: Manipulate a Number  (Read 699 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
God Member
*****
Karma: 19
Posts: 785
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use the % operator.  It gives you the remainder of a division.  If you % by 1000000 then the result will be the last six digits.
Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 106
Posts: 4691
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Use the % operator.

Good thinking, Batman... I was starting to thing strings and right parts and stuff  smiley-yell
Logged

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Guys

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

Cheers

Jase  smiley
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50319
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
long bignum = 2844493234;
int smallernum = bignum % 1000000;
Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 106
Posts: 4691
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 106
Posts: 4691
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Pages: [1]   Go Up
Jump to: