Jump to content

Archived

This topic is now archived and is closed to further replies.

phporcaffeine

PHP and percentage math

Recommended Posts

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.

Share this post


Link to post
Share on other sites
[!--quoteo(post=372305:date=May 8 2006, 12:03 PM:name=phpORcaffine)--][div class=\'quotetop\']QUOTE(phpORcaffine @ May 8 2006, 12:03 PM) [snapback]372305[/snapback][/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.
[code]
$nA = 5;
$nB = 30;
$nC = $nA/$nB; // equals 16
$nD = $nA%$nB // equals 64 (your decimal)
[/code]

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

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]

Share this post


Link to post
Share on other sites
Cripes! Take the simple route
[code]<?php
$num = 100 * 5 / 30; // or whatever
printf("%.2f",$num); // displays 16.67 (the correct answer)
?>[/code]

Share this post


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

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

Share this post


Link to post
Share on other sites
You could make a reusable function like this example:

[code]
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/>';
[/code]

[quote]
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%
[/quote]

Share this post


Link to post
Share on other sites

×

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.