Manipulate a Number

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

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

Use the % operator.

Good thinking, Batman... I was starting to thing strings and right parts and stuff :stuck_out_tongue_closed_eyes:

Hi Guys

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

Cheers

Jase :slight_smile:

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?

long bignum = 2844493234;
int smallernum = bignum % 1000000;

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.

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

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)