# get digits of integer

hi,

i am working on a simple facebook game. facebook store single score integer and i want to store counts of in game purchases in that integer. example; storing that “score: 1230525”. first three digits(“123”) will original score, 4th and 5th digits are count of one of purchased item(“05”) and last two digits for another item count(“25”). but i have no idea how to split that integer properly.

Breaking the score down:

``````// ...
int playerScore = score / 10000; // score is 1230525.
// Divide by 10,000 and you have 123

int itemOne = (score % 10000) / 100; // score is 1230525.
// Modulus of 10,000 is 0525.
// Divide by 100 and you have 05.

int itemTwo = score % 100; // score is 1230525.
// Modulus of 100 is 25.
// ...
``````

Creating the score:

``````score = (playerScore * 10000) + (itemOne * 100) + itemTwo;
// 25 + 0500 + 1230000
``````

Edit: For clarification, the modulus is effectively the remainder after division. With integers, if you divide 128 by 100 (128 / 100), the result is 1. The remainder of 28 would be gotten with the modulus instead (128 % 100). By association, (128 / 100) + (128 % 100) = 128. Rather, the initial number divided by a number, added to itself by the modulus of that number is your original number again.

For what it’s worth, the modulus operation can also be simulated for floats by using Mathf.Repeat().