/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] PHP Rounding

Hey guys…

I am processing some XML data for offsite order fulfillment and I need to round the weights of the items UP to the nearest 0.5lbs.

I know you can round to one decimal place with round, but can I round to the nearest 0.5?

Any ideas?

Thanks!
Joe

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@SodbusterNov 07.2008 — Try this:[code=php]function round_up_by_half($num)
{
$int = floor($num);
$dec = strrchr($num, '.');
if (!$dec || ($dec == '.')) {
return $int . '.0';
} else {
$dec = trim($dec, '.');
if ((($dec > 0) && ($dec[0] <= 4)) || (strrev($dec) == 5)) {
return $int . '.5';
} else {
return ceil($num) . '.0';
}
}
}[/code]
By the way, "up to the nearest" is self-contradictory, so I've assumed you mean "up to the next".
Copy linkTweet thisAlerts:
@joseph_liuauthorNov 07.2008 — Thanks! That worked perfectly!
Copy linkTweet thisAlerts:
@scragarNov 07.2008 — huh, I figured it'd be better to go for something like:
[code=php]function my_round($number, $scope=0.5){
$fraction = fmod($number, $scope);
if($fraction < $scope/2)
return $number - $fraction;
else
return $number + ($scope - $fraction);
}[/code]
I know it's going to use more CPU power, but honestly, I like how much more flexible it is.
Copy linkTweet thisAlerts:
@SodbusterNov 07.2008 — Flexibility is nice, of course, but your function transforms, for example, 15.2 to 15 and 15.59 to 15.5, which apparently is not what OP was after. And if he wishes to alter my function to make it more flexible, he's certainly welcome to.
Copy linkTweet thisAlerts:
@scragarNov 07.2008 — Well if you always want to round up, you could just comment out a few lines:
[code=php]
function my_round($number, $scope=0.5){
$fraction = fmod($number, $scope);
// if($fraction < $scope/2)
// return $number - $fraction;
// else
return $number + ($scope - $fraction);
}[/code]

And if you ever wanted rounding down, it can be done with a similar edit:
[code=php]
function my_round($number, $scope=0.5){
$fraction = fmod($number, $scope);
// if($fraction < $scope/2)
return $number - $fraction;
// else
// return $number + ($scope - $fraction);
}[/code]

but right now I'm defending code I knew wasn't needed, so ignore me if you want ?
Copy linkTweet thisAlerts:
@SodbusterNov 08.2008 — Here's a better version of my original function:[code=php]function round_up_by_half($num)
{
$dec = trim(strrchr($num, '.'), '.0');
if (!$dec) {
return floor($num) . '.0';
} elseif ($dec <= 5) {
return floor($num) . '.5';
} else {
return ceil($num) . '.0';
}
}[/code]
×

Success!

Help @joseph_liu spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...