# PHP and percentage math

5 replies to this topic

### #1 phporcaffeine

phporcaffeine
• Members
• 361 posts
• LocationOhio, USA

Posted 08 May 2006 - 04:03 PM

Does php have a better way of doing percentage math than plain ole' divison? I don't want absurd decimals that I have to deal with, example:

30 database records

5 of the 30 are "special"

5 is "what" percent of 30

now you and I know that is 16 with a remainder (5 / 30 = 16.64)

Is there a better way to resolve that so I don't end up having to figure out the decimal position.
Thanks,

Ryan Huff
President & Founder, MyCodeTree
support@mycodetree.com | http://mycodetree.com

• Members
• 164 posts
• LocationOttawa, On

Posted 08 May 2006 - 04:55 PM

[!--quoteo(post=372305:date=May 8 2006, 12:03 PM:name=phpORcaffine)--][div class=\'quotetop\']QUOTE(phpORcaffine @ May 8 2006, 12:03 PM) [/div][div class=\'quotemain\'][!--quotec--]
Does php have a better way of doing percentage math than plain ole' divison? I don't want absurd decimals that I have to deal with, example:

30 database records

5 of the 30 are "special"

5 is "what" percent of 30

now you and I know that is 16 with a remainder (5 / 30 = 16.64)

Is there a better way to resolve that so I don't end up having to figure out the decimal position.
[/quote]

your working with integers with your example above... if you work with floats, you'll already have the decimal value and won't need to figure it out.
```\$nA = 5;
\$nB = 30;
\$nC = \$nA/\$nB; // equals 16
\$nD = \$nA%\$nB // equals 64 (your decimal)
```

Its easier to work with floats for your situation
```\$fA = 5.0; // the .0 means to create a float
\$fB = 30.0; // another float
\$fC = \$fA/\$fB; // equals 16.64
```

if you want to then start formating your results, lookup the function : [a href=\"http://ca.php.net/manual/en/function.number-format.php\" target=\"_blank\"]number_format()[/a]

### #3 AndyB

AndyB
• Staff Alumni
• 5,465 posts
• LocationToronto

Posted 08 May 2006 - 05:27 PM

Cripes! Take the simple route
```<?php
\$num = 100 * 5 / 30; // or whatever
printf("%.2f",\$num); // displays 16.67 (the correct answer)
?>```

Legend has it that reading the manual never killed anyone.
My site

• Members
• 164 posts
• LocationOttawa, On

Posted 08 May 2006 - 05:31 PM

[!--quoteo(post=372328:date=May 8 2006, 01:27 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ May 8 2006, 01:27 PM) [/div][div class=\'quotemain\'][!--quotec--]
Cripes! Take the simple route
```<?php
\$num = 100 * 5 / 30; // or whatever
printf("%.2f",\$num); // displays 16.67 (the correct answer)
?>```
[/quote]

ahh i forgot to add the *100 to my post.

### #5 toplay

toplay
• Staff Alumni
• 973 posts

Posted 08 May 2006 - 06:44 PM

You could make a reusable function like this example:

```function percent (\$perc, \$dec_points = 0, \$fmt = false) {
\$perc = abs(\$perc + 0.00000001);  // Add a delta
if (\$perc < 1) \$perc *= 100;
\$perc = round(\$perc, \$dec_points);
return \$fmt ? sprintf('%01.' . \$dec_points . 'f%%', \$perc) : \$perc;
}

\$result = (float) 5 / 30;

echo "Result: \$result <br/>";

echo 'As percent to 2 decimal places: ', percent(\$result, 2), '<br/>';

echo 'As percent rounded to an integer: ', percent(\$result), '<br/>';

echo '12.345 percent rounded to 2 decimal places: ', percent(12.345, 2), '<br/>';

echo 'Format 54.321 percent to 1 decimal places: ', percent(54.321, 1, true), '<br/>';

echo 'Format 26.65 percent as integer: ', percent(26.65, 0, true), '<br/>';
```

Result: 0.16666666666667
As percent to 2 decimal places: 16.67
As percent rounded to an integer: 17
12.345 percent rounded to 2 decimal places: 12.35
Format 54.321 percent to 1 decimal places: 54.3%
Format 26.65 percent as integer: 27%

### #6 phporcaffeine

phporcaffeine
• Members
• 361 posts
• LocationOhio, USA

Posted 08 May 2006 - 09:43 PM

Thank you All!

Everyone's input has taught me something useful!
Thanks,

Ryan Huff
President & Founder, MyCodeTree
support@mycodetree.com | http://mycodetree.com

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users