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.

# PHP and percentage math

Started by phporcaffeine, May 08 2006 04:03 PM

5 replies to this topic

### #1

Posted 08 May 2006 - 04:03 PM

### #2

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.

Its easier to work with floats for your situation

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]

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]

### #4

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

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

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

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

Posted 08 May 2006 - 09:43 PM

Thank you All!

Everyone's input has taught me something useful!

Everyone's input has taught me something useful!

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

0 members, 0 guests, 0 anonymous users