jkkenzie Posted September 3, 2009 Share Posted September 3, 2009 Hi! I would like to split a number into groups of three starting from the right Just like in currency BUT i want i want to zero to the right ti indicate their position in the number i.e tens, hundreds ... and save each group in variable e.g I have 525 as my number. I want to split it to 500 , 20 and 5 separately in variables so as to use it. (table below): Milllions thousands Hundreds Tens Ones xxxxxxxxxx xxxxxxx 500 20 5 Quote Link to comment https://forums.phpfreaks.com/topic/172979-solved-splitting-numbers/ Share on other sites More sharing options...
Daniel0 Posted September 3, 2009 Share Posted September 3, 2009 Just divide by multiples of 10. Quote Link to comment https://forums.phpfreaks.com/topic/172979-solved-splitting-numbers/#findComment-911673 Share on other sites More sharing options...
jkkenzie Posted September 3, 2009 Author Share Posted September 3, 2009 It will give me decimal places. An example would do good. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/172979-solved-splitting-numbers/#findComment-911678 Share on other sites More sharing options...
Daniel0 Posted September 3, 2009 Share Posted September 3, 2009 $hundreds = (int) $number / 100; That'll give you how many hundreds there are. Quote Link to comment https://forums.phpfreaks.com/topic/172979-solved-splitting-numbers/#findComment-911680 Share on other sites More sharing options...
jkkenzie Posted September 3, 2009 Author Share Posted September 3, 2009 It wont work with my scenario, Like 525 is giving me 5 for hundreds and 52 for tens and 525 for ones AND yet I wanted. 500 for hundreds and 20 for tens and 5 for ones. Quote Link to comment https://forums.phpfreaks.com/topic/172979-solved-splitting-numbers/#findComment-911702 Share on other sites More sharing options...
Daniel0 Posted September 3, 2009 Share Posted September 3, 2009 Same concept. <?php $number = 525; $parts = array(); $div = pow(10, strlen($number) - 1); $n = $number; while ($div >= 1) { $parts[$div] = (int) ($n / $div) * $div; $n = $n % $div; $div /= 10; } print_r($parts); I just want to point out that it is mathematically incorrect to say that there are 500 hundreds in the number 525 though. 525 = 5*102 + 2*101 + 5*100 Quote Link to comment https://forums.phpfreaks.com/topic/172979-solved-splitting-numbers/#findComment-911716 Share on other sites More sharing options...
jkkenzie Posted September 3, 2009 Author Share Posted September 3, 2009 You are right it is mathematically incorrect to say that there are 500 hundreds in the number 525. I was not suggesting that but it was the only way to quickly let you know what i wanted directly. In our banks here, we have a deposit slip that has a table for 10, 100, 1000, 10000 ..... ans so on, you have to indicate the value of the NOTE for each category, If you have 525 i.e a note for 500 and 20 shillings coin and 5 shillings coin. you indicate under 100 as 500 and so on. I dont think i am clear BUt something like that, it just an input form to take care of data integrity. Quote Link to comment https://forums.phpfreaks.com/topic/172979-solved-splitting-numbers/#findComment-911724 Share on other sites More sharing options...
PFMaBiSmAd Posted September 3, 2009 Share Posted September 3, 2009 <?php $value = 1234567890; $array = str_split($value); // break into characters $array = array_reverse($array); // reverse the order (1's, 10's, 100's...) $base = 1; // current base 1,10,100... $result = array(); // some place to store the results foreach($array as $value){ $result[$base] = $value * $base; $base *= 10; // mult by 10 } echo "<pre>",print_r($result,true),"</pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/172979-solved-splitting-numbers/#findComment-911733 Share on other sites More sharing options...
jkkenzie Posted September 3, 2009 Author Share Posted September 3, 2009 That is great stuff, thanks alot guys they all work perfect. Quote Link to comment https://forums.phpfreaks.com/topic/172979-solved-splitting-numbers/#findComment-911752 Share on other sites More sharing options...
Psycho Posted September 3, 2009 Share Posted September 3, 2009 I took a different approach and processed the value as a string (first converting the value to an int and then itno a string to remove decimal content): <?php function convertNumToParts($number) { $numberStr = (string) (int) $number; $result = array(); for($i=0; $i<strlen($numberStr); $i++) { $result[pow(10, strlen($numberStr)-$i-1)] = substr($numberStr, $i, 1); } return $result; } $number = 525; $parts = convertNumToParts($number); print_r($parts); //Output: //Array //( // [100] => 5 // [10] => 2 // [1] => 5 //) ?> Quote Link to comment https://forums.phpfreaks.com/topic/172979-solved-splitting-numbers/#findComment-911753 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.