Jump to content

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;
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.