Jump to content

[SOLVED] Round to 0.5, 1.5, 2.5 etc


fri3ndly

Recommended Posts

Hello

 

I have a star rating system which will look up ratings that will be '2.3', '0.7' etc

 

I am trying to round up to the nearest half number as I have half stars to show if this is the case...

 

so... 2.3 > 2.5, 0.7 > 1  etc etc

 

Please can someone help as round or ceil arent helping :(

Link to comment
https://forums.phpfreaks.com/topic/112772-solved-round-to-05-15-25-etc/
Share on other sites

<?php
   $rating=2.4;
   $rounded = round($rating*2)/2;
?>

This code would round 0.7 down because it is less than halfway to 1 from 0.5. It seems to me that you would want to round .6 and .7 down and .8 and .9 up, but if you really want to round .7 up you can just check for it like this:

<?php
$rating=2.7;
if (($rating*10)%10 == 7)
$rounded = $rating+0.3;
else
$rounded = round($rating*2)/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.