View Single Post
  #6  
Old 01-16-2014, 09:28 PM
callimuc callimuc is offline
ジ
callimuc's Avatar
Join Date: Nov 2010
Location: Germany
Posts: 1,015
callimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to behold
Quote:
Originally Posted by BlueMelon View Post
Since we are dealing with numbers, I would approach it differently with some simple math.

PHP Code:
function numberToArray(temp.n) {
  
temp.n = abs(temp.n);
  
temp.digits = {};
  while(
temp.n != 0) {
    
temp.digits.insert(0, temp.n % 10);
    
temp.n = int(temp.n/10);
  }
  return 
temp.digits;
}

function 
onCreated() {
  
temp.fullnumber = 482;

  
temp.digits = numberToArray(temp.fullnumber);
  echo(
temp.digits[0]); // 4
  
echo(temp.digits[1]); // 8
  
echo(temp.digits[3]); // 2
} 
If you want it the other way (reversed), change the .insert(0, ...) with .add(...)
Probably would have done something like
PHP Code:
function numberToArray(n) {
  
temp.n = abs(temp.n); //abs() will always keep your number positive
  
temp.toReturn = NULL;
  for (
temp.i=0; temp.i<temp.n.length(); temp.i++) {
    
temp.toReturn.add(temp.n.substring(temp.i, 1)); //first add 4, then 8, then 2 (in your example);
    /*
    If you would like to have the 2 first, then 8 and then 4 (numbers taken from your example again), then just replace
    temp.toReturn.add(temp.n.substring(temp.i, 1));
    with
    temp.toReturn.insert(0, temp.n.substring(temp.i, 1));
    */
  
}
  return 
temp.toReturn;
} 
__________________
MEEP!
Reply With Quote