Jump to content

Round to the nearest 1/8"


danscreations

Recommended Posts

mayby something like

val=9.3;

nval=int((val*+.5) /8;
echo nval;

I think that shud work.

 

why the * 8 and / 8? (.5 is used for rounding)

 

A trick i picked up here in the forums

 

to round every half, u can get the rounded version of the number doubled (*2)

 

so to get 8ths, u need to double our double and doublt it again

 

*2 = halfs

*4 = quarters

*8 = Eights

*16 = Sixteenths

If you looked at the manual page for round, the first example is probably what you're looking for:

<?php
function round_to($number, $increments) {
$increments = 1 / $increments;
return (round($number * $increments) / $increments);
}
?>
For example:
<?php
$n = 5.3;
echo round_to($n, 0.5); // 5.5
?>

 

Ken

or you could get all complicated and do something like:

 

<?php
$n = 3.3912;
$rounded = round($n,3); //shorten to 3 decimal places
$parts = explode(".",$rounded); 
$decimal = $parts[1];  // = 391
$divided = round($decimal/ 125);   // = 3
$new_decimal = $divided * .125;  // = 0.375
$n = $parts[0] + $new_decimal;  // = 3.375
echo $n;
?>

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.