Jump to content

[SOLVED] splitting numbers


jkkenzie

Recommended Posts

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

<?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>";
?>

Link to comment
Share on other sites

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
//)

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.