phporcaffeine Posted May 8, 2006 Share Posted May 8, 2006 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 records5 of the 30 are "special"5 is "what" percent of 30now 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 Link to comment Share on other sites More sharing options...
lead2gold Posted May 8, 2006 Share Posted May 8, 2006 [!--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 records5 of the 30 are "special"5 is "what" percent of 30now 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] Quote Link to comment Share on other sites More sharing options...
AndyB Posted May 8, 2006 Share Posted May 8, 2006 Cripes! Take the simple route[code]<?php$num = 100 * 5 / 30; // or whateverprintf("%.2f",$num); // displays 16.67 (the correct answer)?>[/code] Quote Link to comment Share on other sites More sharing options...
lead2gold Posted May 8, 2006 Share Posted May 8, 2006 [!--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 whateverprintf("%.2f",$num); // displays 16.67 (the correct answer)?>[/code][/quote]ahh i forgot to add the *100 to my post. Quote Link to comment Share on other sites More sharing options...
toplay Posted May 8, 2006 Share Posted May 8, 2006 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.16666666666667As percent to 2 decimal places: 16.67As percent rounded to an integer: 1712.345 percent rounded to 2 decimal places: 12.35Format 54.321 percent to 1 decimal places: 54.3%Format 26.65 percent as integer: 27%[/quote] Quote Link to comment Share on other sites More sharing options...
phporcaffeine Posted May 8, 2006 Author Share Posted May 8, 2006 Thank you All!Everyone's input has taught me something useful! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.