Welcome, Guest. Please login or register.
Did you miss your activation email?
May 26, 2012, 06:44:25 AM

Login with username, password and session length
Search:     Advanced search
Interested in joining the WebsiteBaker team?
For more Information read here or on our new website.
155534 Posts in 21713 Topics by 7737 Members
Latest Member: chris85
* Home Help Search Login Register
Pages: [1]   Go Down
Print
Author Topic: percentage droplet  (Read 687 times)
snark
Guest
« on: June 16, 2010, 10:32:56 AM »

the following script calculates a percentage:

Code:
<?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:
<?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?




Logged
DarkViper
Development Team
*****
Offline Offline

Posts: 1252


« Reply #1 on: June 16, 2010, 12: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)
<?php

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

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

?>

have fun.....
« Last Edit: June 17, 2010, 06:26:24 PM by DarkViper » Logged

Anleitungen lesen und selber nachdenken ist anstrengend...  Da lass ich doch lieber andere für mich denken...

In 1984:  Nineteen Eighty-Four is a unrealistic utopia!!
In 2012:  Nineteen Eighty-Four is a little piece only of our reality!!
snark
Guest
« Reply #2 on: June 16, 2010, 01:11:39 PM »

looks great but it returns a "0" as percentage to everything I fill in ...

Logged
pcwacht
AddOn Development
*
Offline Offline

Posts: 2859



WWW
« Reply #3 on: June 16, 2010, 01:22:58 PM »

Typo?

Code:
<?php

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

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

?>
Code:
<?php

  $basevalue  
= isset($basevalue) ? (float)$basevalue 0;
  if( !
$basevalue ) return '0';  // Division by zero protection
  
$percentvalue = isset($percentvalue) ? (float)$percentvalue 0;
  if( !
$percentvalue ) return '0';
  
$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
Logged

http://www.ictwacht.nl = Dutch ICT info
http://www.pcwacht.nl = My first
both still work in progress, since years.....
snark
Guest
« Reply #4 on: June 16, 2010, 03:16:53 PM »

helemaal te gek!

dankjewel
Logged
DarkViper
Development Team
*****
Offline Offline

Posts: 1252


« Reply #5 on: June 16, 2010, 03:58:04 PM »

ok, ok.... for the next time i've cleaned the keyboard of my laptop...   rolleyes
Logged

Anleitungen lesen und selber nachdenken ist anstrengend...  Da lass ich doch lieber andere für mich denken...

In 1984:  Nineteen Eighty-Four is a unrealistic utopia!!
In 2012:  Nineteen Eighty-Four is a little piece only of our reality!!
pcwacht
AddOn Development
*
Offline Offline

Posts: 2859



WWW
« Reply #6 on: June 16, 2010, 04:01:40 PM »

no problem, my fingers are dyslexic too Wink
Logged

http://www.ictwacht.nl = Dutch ICT info
http://www.pcwacht.nl = My first
both still work in progress, since years.....
Pages: [1]   Go Up
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!