When you develop an application or a game in which you have to manage some sort of score system it is important to make all the data simply recognizable. For example in our new android game FillTheGap is available an Online Ranking in which every player has his own score. To make every score readable we developed this script and today we are going to explain you how it works!

 



WHAT IS IT

This simple script allows you to convert a number from this:

1234567

To this:

1'234'567

As you can notice the second number looks much more recognizable. When you develop an application is crucial to make it as much simple as possible for your user to understand. Our script has the purpose to do this for you!

For example, take a look at these two images:

screenRanking2 screenRanking1

As you can see the scores of the second one are much more easy to visualize than the scores in the first one!

By the way, if you want to download FillTheGap you can click on this link below
FillTheGap

HOW DOES IT WORKS

  • Get the amount of digits of the number
    You can simply calculate the logarithm to the base 10 of the number itself. Since we need an integer value we have to add 1 to the result and then use the floor function to get the actual digits amount.
    DgNum = floor(log10(1234567) + 1))
    DgNum = floor(6.092 + 1)
    DgNum = 7
  • 'For' cicle on every digit
    To covert the number we need to add an ' every three digits. We can use a 'for' cicle to get every single digits. We introduce two new variables: we use the varaible 'ten' to get the single digits of the whole number; we use the variable 'cont' to know when to add an ' (every three digits).
    • Get single digits
      To get the every single digit we need to divide the number by the variable 'ten' and then calculate the modulus by 10. Take a look on this example on the number 1234567:
      Dg1 = (1'234'567 / 1'000'000) % 10 = 1
      Dg2 = (1'234'567 / 100'000) % 10 = 2
      Dg3 = (1'234'567 / 10'000) % 10 = 3
      Dg4 = (1'234'567 / 1'000) % 10 = 4
      Dg5 = (1'234'567 / 100) % 10 = 5
      Dg6 = (1'234'567 / 10) % 10 = 6
      Dg7 = (1'234'567 / 1) % 10 = 7
    • Add the '
      If 'cont' % 3 is equal to 0 we need to add an ' to the number. Otherwise we simply add the digit to the result number.
      cont = 7 (7 % 3 != 0) -> 1
      cont = 6 (6 % 3 == 0) -> 1'2
      cont = 5 (5 % 3 != 0) -> 1'23
      cont = 4 (4 % 3 != 0) -> 1'234
      cont = 3 (3 % 3 == 0) -> 1'234'5
      cont = 2 (2 % 3 != 0) -> 1'234'56
      cont = 1 (1 % 3 != 0) -> 1'234'567

 

THE SCRIPT

We wrote this script in PHP and JAVASCRIPT. As always it is all free and open-source!

 


// Get number from an input field
$number = $_GET['number'];

// Get amount of digits
$cif = floor(log10($number) + 1);

// If number is 0 then return 0
if ($number == 0) {
 echo "0";
}
else {
 $final = "";

 // For cicle to get every single digits and add the '
 for ($ten = pow(10, ($cif - 1)), $cont = $cif; $cont > 0; $ten /= 10, --$cont) {
  if ($cont % 3 == 0 && $cont != $cif) {
   $final .= "'";
  }

  $single = floor(($number / $ten) % 10);
  $final .= $single;
 }

 // Print the result
 echo $final;
}

 


// Get number from an input field
var number = document.getElementById('number').value;

// Get amount of digits
var cif = Math.floor(Math.log10(number) + 1);

// If number is 0 then return 0
if (number == 0) {
 document.getElementById('numResult').innerHTML = '0';
}
else {
 var final = "";

 // For cicle to get every single digits and add the '
 for (var ten = Math.pow(10, (cif - 1)), cont = cif; cont > 0; ten /= 10, --cont) {
  if (cont % 3 == 0 && cont != cif) {
   final += "'";
  }

  var single = Math.floor(((number / ten) % 10));
  final += single;
 }

 // Print the result
 document.getElementById('numResult').innerHTML = final;
}