Jump to content

math round


denoteone

Recommended Posts

try this.. its not tested

 

<?php
function roundP5($num) {
if ($d = stristr($num,'.')) {
	$place = substr($d,1);
	if (strlen($place) == 1) $place = $place * 10;
	$zpad = str_repeat('0',strlen($place) - 2);
	$main = stristr($d,'.',true);
	if (!$main) $main = 0;
	if ($place >= ("75".$zpad)) {
		$place = 0;
		$main++;
	}
	elseif ($place >= ("25".$zpad)) {
		$place = 5;
	} elseif ($place < ("25".$zpad)) {
		$place = 0;
	}
	return $main.$place;
}
return false;
}
?>

Link to comment
Share on other sites

I believe ya wud have to strip the whole number and than work with the rounding for the fraction...

$num=1.74;
$fract=$num-abs($num);

 

than the easiest way is using if..else... construct

if($fract>=.75) $fract=1;
elsrif($fract>=.25) $fract=.5;
else $fract=0;

 

than add the whole number and the new fraction together

$num=abs($num)+$fract;

 

well thats the simplest I can think of at the moment;

a lot of this can be cut down significantly, but wasnt to keep example of how it can be done and still readable;

 

$num=1.24999;
$num=(abs($num))+(($fract=$num-abs($num))>=.75?1:($fract>=.26?.5:0)));

 

careful that was done off the top of my head so there maybe a missing ( or ) but ya gets the idea :)

 

Link to comment
Share on other sites

function roundP5($num) {
if ($d = stristr($num,'.')) {
	$place = substr($d,1);
	if (strlen($place) == 1) $place = $place * 10;
	$zpad = str_repeat('0',strlen($place) - 2);
	$main = substr($num,0,strpos($num,'.',0));
	if (!$main) $main = 0;
	if ($place >= ("75".$zpad)) {
		$place = 0;
		$main = $main + 1;
	}
	elseif ($place >= ("25".$zpad)) {
		$place = 5;
	} elseif ($place < ("25".$zpad)) {
		$place = 0;
	}
	return $main.".".$place;
}
return false;
}

 

this is working.. I just tested it

Link to comment
Share on other sites

This code implements MS Excel's MROUND() function, which accepts a number to round, and a precision to round it to.

function SIGN($number) {
if ($number == 0.0) {
	return 0;
}
return $number / abs($number);
}

function MROUND($number,$multiple) {

if ((SIGN($number)) == (SIGN($multiple))) {
	$lowerVal = floor($number / $multiple) * $multiple;
	$upperVal = ceil($number / $multiple) * $multiple;
	$adjustUp = abs($number - $upperVal);
	$adjustDown = abs($number - $lowerVal) + PRECISION;
	if ($adjustDown < $adjustUp) {
		return $lowerVal;
	}
	return $upperVal;
}
return False;
}

echo MROUND(123.45,0.5);

Link to comment
Share on other sites

hes trying to round to .5 intervals..

 

....

 

thats why I wrote the function, everybody knows about round()

 

EDIT::

 

oo I see what sasa did :)

 

*** RussellReal extracts foot from mouth ***

 

*** And then inserts directly up the rectum of mjdamato ***

 

Grow up, it was just some good natured ribbing. Didn't mean to offend

 

 

@Mark Baker: You could accomplish the same thing more efficiently using the same logic Sasa provided:

function MROUND($num, $unit)
{
    if ($unit==0) { return $num; }
    $multiplier = 1 / $unit;
    return round($num * $multiplier ) / $multiplier;
}

Link to comment
Share on other sites

hes trying to round to .5 intervals..

 

....

 

thats why I wrote the function, everybody knows about round()

 

EDIT::

 

oo I see what sasa did :)

 

*** RussellReal extracts foot from mouth ***

 

*** And then inserts directly up the rectum of mjdamato ***

 

Grow up, it was just some good natured ribbing. Didn't mean to offend

 

 

yeahhh... lol

Link to comment
Share on other sites

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.