WebsiteBaker Community Forum

WebsiteBaker Support (2.8.x) => Droplets & Snippets => Topic started by: snark on June 16, 2010, 11:32:56 AM

Title: percentage droplet
Post by: snark on June 16, 2010, 11:32:56 AM
the following script calculates a percentage:

Code: [Select]
<?php
function percent($num_amount$num_total) {
$count1 $num_amount $num_total;
$count2 $count1 100;
$count number_format($count20);
echo 
$count;
}
?>


which can be called by:

Code: [Select]
<?php percent(14412);?>

now I want to use this as a droplet which will app. be something like:
[[percentage?firstnumber=144&secondnumber=12]]

is there anyone who can help me here?




Title: Re: percentage droplet
Post by: DarkViper on June 16, 2010, 01:16:07 PM
Droplet:  [percentage]

  [[percentage?basevalue=200&percentvalue=20&decimals=0]]

  20€ of  200€ are 10%

  [[percentage?basevalue=55&percentvalue=5&decimals=4]]

  5€ of 55€ are 9.0909%

The Droplet can handle integer and floatingpoint also
The number of decimals can be between 0 and 10

Code: (Droplet percentage) [Select]
<?php

  $basevalue  
= isset($basevalue) ? (float)$basevalue 0;
  if( !
$basevalue ) return &#39;0&#39;;  // Division by zero protection
  
$percentvalue = isset($percentvalue) ? (float)$percentvalue 0;
  if( !
$percentvalue ) return &#39;0&#39;;
  
$decimals = isset($decimals) ? (intval($decimals) % 11) : 0;

  return  
number_format( (( 100 $percentvalue ) / $basevalue ), $decimals );

?>

have fun.....
Title: Re: percentage droplet
Post by: snark on June 16, 2010, 02:11:39 PM
looks great but it returns a "0" as percentage to everything I fill in ...

Title: Re: percentage droplet
Post by: pcwacht on June 16, 2010, 02:22:58 PM
Typo?

Code: [Select]
<?php

  $basevalue  
= isset($basevalue) ? (float)$basevalue 0;
  if( !
$basevalue ) return &#39;0&#39;;  // Division by zero protection
  
$percentvalue = isset($percentvalue) ? (float)$percenvalue 0;
  if( !
$percenvalue ) return &#39;0&#39;;
  
$decimals = isset($decimals) ? (intval($decimals) % 11) : 0;

  return  
number_format( (( 100 $percentvalue ) / $basevalue ), $decimals );

?>
Code: [Select]
<?php

  $basevalue  
= isset($basevalue) ? (float)$basevalue 0;
  if( !
$basevalue ) return &#39;0&#39;;  // Division by zero protection
  
$percentvalue = isset($percentvalue) ? (float)$percentvalue 0;
  if( !
$percentvalue ) return &#39;0&#39;;
  
$decimals = isset($decimals) ? (intval($decimals) % 11) : 0;

  return  
number_format( (( 100 $percentvalue ) / $basevalue ), $decimals );

?>
twice the missing 't' in percentvalue

Didn't test it...
Have fun,
John
Title: Re: percentage droplet
Post by: snark on June 16, 2010, 04:16:53 PM
helemaal te gek!

dankjewel
Title: Re: percentage droplet
Post by: DarkViper on June 16, 2010, 04:58:04 PM
ok, ok.... for the next time i've cleaned the keyboard of my laptop...   :roll:
Title: Re: percentage droplet
Post by: pcwacht on June 16, 2010, 05:01:40 PM
no problem, my fingers are dyslexic too ;)