Go Down

Topic: Manipulate a Number (Read 806 times) previous topic - next topic

ilovetoflyfpv

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

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

JimboZA

Quote
Use the % operator.


Good thinking, Batman... I was starting to thing strings and right parts and stuff  :smiley-yell:
Arduino ethernet server here.... http://jimboza.gotdns.com:8085/

No PMs for help please

ilovetoflyfpv

Hi Guys

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

Cheers

Jase  :)

PaulS

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

Code: [Select]
long bignum = 2844493234;
int smallernum = bignum % 1000000;

JimboZA

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.
Arduino ethernet server here.... http://jimboza.gotdns.com:8085/

No PMs for help please

ilovetoflyfpv

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

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)
Arduino ethernet server here.... http://jimboza.gotdns.com:8085/

No PMs for help please

Go Up