Author Topic: Simple math equation help.  (Read 1035 times)

0 Members and 1 Guest are viewing this topic.

Offline butsagsTopic starter

  • Irregular
  • Posts: 33
    • View Profile
Simple math equation help.
« on: August 28, 2010, 01:38:29 AM »
Hey guys,
So i have a variable that i want to check. I want the script to check if a number is over 150 then break it down into multiple numbers each up to 150.
so if the number is 900, there will be six chunks of 150. if the number is say 800, there will be 5 chunks of 150 and one chunk of 50. etc

something like:


<?php
if($OrderWeight 150){
$OrderWeight explode("150"'$OrderWeight');
$i=0;
while (
$i $OrderWeight) {
$OrderWeight[$i];
}
} else {

}
?>

Thanks Guys.
I feel like this is a very simple equation, I'm just missing a simple php function

Online DavidAM

  • Devotee
  • Posts: 1,026
  • Gender: Male
    • View Profile
Re: Simple math equation help.
« Reply #1 on: August 28, 2010, 02:12:55 AM »
$chunks int($OrderWeight 150);
$leftover $OrderWeight 150;
-- I haven't lost my mind, it's backed up on tape ... somewhere!

Offline butsagsTopic starter

  • Irregular
  • Posts: 33
    • View Profile
Re: Simple math equation help.
« Reply #2 on: August 28, 2010, 07:28:02 AM »
Thanks for your reply man,
im getting an error on $chunks int($OrderWeight 150);
I think because int cant be used by itself like that. the error is saying its an undefined function ha.
and presuming that that worked,
now how would i print out however many chunks of 150 or less there are? something like an array and then a while (($i=0;$i<count($chunkarray);$i++)){echo "".$chunkarray[$i]."";}

Online DavidAM

  • Devotee
  • Posts: 1,026
  • Gender: Male
    • View Profile
Re: Simple math equation help.
« Reply #3 on: August 28, 2010, 01:11:18 PM »
oops, sorry.  I keep forgetting that int is not a function in php.  It should be intval()
$chunks intval($OrderWeight 150);
$leftover $OrderWeight 150;


This gives you a count of the number of times your multiplier is in the total and a leftover.  To output it, you would print the multiplier $chunks times and then print the leftover:
$mult 150;
	
// Our multiple for breaking down the value
$total 340;
	
// The value we are starting with

$chunks intval($total $mult);
	
// How many multiples are in the total
$left $total $mult;
	
	
// What is left over (modulus operator)

for ($i 1$i <= $chunks$i++) {
	
printf('..%d..'$mult);
}
if (
$left 0printf('..%d..'$left);
echo 
PHP_EOL?>

OUTPUT: ..150....150....40..


If you need the values in an array for some reason, you can do something like this:
$mult 150;
	
// Our multiple for breaking down the value
$total 340;
	
// The value we are starting with

$chunks = array();
	
// Start with an empty array
$breakDown $total;  // Just so we don't wipe out our starting value
while ($breakDown 0) {
	
if (
$breakDown >= $mult) {
	
	
$chunks[] = $mult;
	
	
$breakDown -= $mult;
	
} else {
	
	
$chunks[] = $breakDown;
	
	
$breakDown -= $breakDown;
	
}
}

printf('%d contains the following: '$total);
foreach(
$chunks as $chunk
	
printf('..%d..'$chunk);
	

print 
PHP_EOL;?>

OUTPUT: 340 contains the following: ..150....150....40..
-- I haven't lost my mind, it's backed up on tape ... somewhere!

Offline butsagsTopic starter

  • Irregular
  • Posts: 33
    • View Profile
Re: Simple math equation help.
« Reply #4 on: August 30, 2010, 12:26:47 AM »
Thanks man it worked perfectly. I didn't even know that such a thing as modulus exsisted (-__-) i need to go to college :)

Hhaha,
Thanks for your help my friend.