Jump to content

[SOLVED] Slicing a number :S


Tiltan

Recommended Posts

Ok i dident know how to name this topic  :-\

 

 

Lets say a form is submited (So i dont know what the numbers will be...)

 

xxx * xxxxx =  x'xxx'xxx'xxx

Lets just say:  3'496'562'654

 

How can i get the result to be put like this:

 

654

562

496

  3

 

I want the result to be cut from behind every 3rd numeral, no matter what the result is.

Link to comment
Share on other sites

Just an idea :

 

$number = "333'333'333";
$number = explode("'", $number);
foreach($number as $key => $value)
{
$position = $key + 1;
$position = $position / 3;
if(is_int($position))
{
echo $value.'<br />';
}
else
{
echo $value;
}
}

Link to comment
Share on other sites

Meh. He put quotations in the original code - I just worked with that. It wouldn't be too hard to do it without though. Instead of exploding it, just reverse it (not sure about ints, but for strings you can use strrev();) and then something like this maybe?

 

$number = 243423;
$length = strlen($number);
for($i = 0; $i <= $length; $i++)
{
$key = ceil($i / 3);
$array[$key] .= substr($number, $i, 1);
}

foreach($array as $key => $value)
{
echo $value.'<br />';
}

Link to comment
Share on other sites

This is the best I can do...

$number='1234567890';

$arrSplit=splitNumber($number);

function splitNumber($num) {
  $revNumber=revstr($num);
  $len=strlen($num);
  $tmp='';
  for ($i=0;$i<$len;++$i) {
    $tmp.=$revNumber[$i];
    if ($i>1&&$i%3==0) {$tmp.='*';}
  }
  return explode('*',revstr($tmp));
}

Link to comment
Share on other sites

Having to re-post as I just tried my code and got the strrev() messed up and the last item had 4 digits.

$number=1234567890;

$arrSplit=splitNumber($number);
var_dump($arrSplit);

function splitNumber($num) {
  $revNumber=strrev($num);
  $tmp='';
  for ($i=0;$i<strlen($num);++$i) {
    $tmp.=$revNumber[$i];
    if ($i>1 && ($i+1)%3 == 0) {$tmp.='*';}
  }
  return explode('*',strrev($tmp));
}

Output from var_dump():

array(4) { [0]=>  string(1) "1" [1]=>  string(3) "234" [2]=>  string(3) "567" [3]=>  string(3) "890" }

Link to comment
Share on other sites

Ok great, atleast you know what i mean :D

But i am guite new to this with php.

 

I tried to use jackpf's code

$number = 243423;

$length = strlen($number);

for($i = 0; $i <= $length; $i++)

{

$key = ceil($i / 3);

$array[$key] .= substr($number, $i, 1);

}

 

foreach($array as $key => $value)

{

echo $value.'<br />';

}

 

If the $number = 1232766

 

It will split it up to 766 and 232 and 1. Great thats exactly what i wanted. Now.

 

How can i get them in to $a, $b and $c for example, depending on how many parts. There could be like 1 to 10 parts. (this is were my lack of knowledge kicks in... (again))

 

And this...

You're all making it needlessly more complex that it has to be.

 

$parts = explode("'", number_format($number, 0, '.', "'"));

 

works aswell. But i still cant use the parts :,(

i used

foreach($parts as $key => $value)
{
echo $value.'<br />';
}

Link to comment
Share on other sites

You're all making it needlessly more complex that it has to be.

 

$parts = explode("'", number_format($number, 0, '.', "'"));

 

Lol - it's fun though.

 

And I'm not sure exactly what you mean by "put them into $a $b and $c"...

Link to comment
Share on other sites

You're all making it needlessly more complex that it has to be.

 

$parts = explode("'", number_format($number, 0, '.', "'"));

 

works aswell. But i still cant use the parts :,(

i used

foreach($parts as $key => $value)
{
echo $value.'<br />';
}

 

foreach(explode("'", number_format(1234567890, 0, '.', "'")) as $key => $value)
{
echo $value . PHP_EOL;
}

outputs

 

1

234

567

890

 

What else do you want?

 

If you want it reversed then just do:

 

$parts = explode("'", number_format(1234567890, 0, '.', "'"));
for ($i = sizeof($parts) - 1; $i >= 0; $i--) {
echo $parts[$i] . PHP_EOL;
}

Link to comment
Share on other sites

OK thx, Got it now :D

 

Here is what i created. Completly useless but still fun. Feel free to try it.

The result will allways be 999 :D

 

 


Game with 999
<form method="post" action="math.php">
<table>
  <tr><input type="hidden" name="calc" value="calc">
<td><input type="hidden" size="10" maxlength="10" name="a" value="999">999 *</td>
<td><input type="text" size="10" maxlength="10" name="b" value=""></td>
<td><input type="submit" value="submit" name="submit"></td>
  </tr>
</table>
</form>


<?php
if(isset($_POST['calc'])){
$a = $_POST['a'];
$b = $_POST['b'];

echo $a .' * '. $b .'';

$number = $a * $b;
echo ' = '.$number .'<br/><br/>';

$parts = explode("'", number_format($number, 0, '.', "'"));	

foreach($parts as $key => $value)
{
echo '+ '. $value.'<br />';
}


$s = $parts[0] + $parts[1] + $parts[2] + $parts[3] + $parts[4] + $parts[5];

if($s == 999){
echo '= '. $s;
}

else{
echo '= '. $s;
echo '<br />';
while($s > 999){
	$s = $parts = explode("'", number_format($s, 0, '.', "'"));	

foreach($s as $key => $value)
{
echo '+ '. $value.'<br />';
}


$s = $s[0] + $s[1] + $s[2] + $s[3] + $s[4] + $s[5];
}

echo '= '. $s;
}
}
?>

Link to comment
Share on other sites

What i ment with the $a $b $c part

How can i get them in to $a, $b and $c for example, depending on how many parts. There could be like 1 to 10 parts. (this is were my lack of knowledge kicks in... (again)

 

Is this

foreach($s as $key => $value)

{

echo '+ '. $value.'<br />';

}

 

$s = $s[0] + $s[1] + $s[2] + $s[3] + $s[4] + $s[5];

 

}

 

But would it be possibly to get it dynamicly? Now i just created 5. knowing that if they re empty they wont count. But there are other situations...

Link to comment
Share on other sites

i locked the form to 10 numbers. But if it would be 30 numbers long.

 

It could look like this 999 999 999 999 999 999 999 999

 

clearly          $s = $s[0] + $s[1] + $s[2] + $s[3] + $s[4] + $s[5];

wouldent be enough since there are 8 parts, not 6

 

What i would like to know if there is a way to create enough "$s[0]" automaticly, instead of guessing?

 

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.