Jump to content

Working out percentages


graham23s

Recommended Posts

Hi Mate,

 

thanks for that! i did this:

 

<?php

$low_value = 2.99;
$high_value = 5.99;

$percent = ($low_value / $high_value) * 100;

$p = number_format($percent);

print "You have saved $p%!";

?>

 

it gives me 50% saving is it ok to round it using number format do you think?

 

cheers mate

 

Graham

use round() to round up, or ceil() to round down.

 

number_format() is for large numbers to put the comma in after every 3 digits. like 1,000,000

 

Regards ACE

 

floor() rounds down. 

ceil() rounds up. 

round() rounds to nearest int.

 

<?php
for ($x = 1.1; $x < 2; $x += .1) {
   echo "[$x] floor " . floor($x) . " | ceil " . ceil($x) . " | round " . round($x) . "<br />";     
}
?>

 

output:

[1.1] floor 1 | ceil 2 | round 1

[1.2] floor 1 | ceil 2 | round 1

[1.3] floor 1 | ceil 2 | round 1

[1.4] floor 1 | ceil 2 | round 1

[1.5] floor 1 | ceil 2 | round 2

[1.6] floor 1 | ceil 2 | round 2

[1.7] floor 1 | ceil 2 | round 2

[1.8] floor 1 | ceil 2 | round 2

[1.9] floor 1 | ceil 2 | round 2

 

Archived

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

×
×
  • Create New...

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.