Round number to closest half

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tonchily
Forum Commoner
Posts: 54
Joined: Thu Sep 02, 2010 10:44 am

Round number to closest half

Post by tonchily »

I'll show an example of I need to be done and I'm sure you'll get what I mean:

$number1 = 1.56 // that number needs to be rounded to 1.55
$number2 = 1.574 // that number needs to be rounded to 1.55 aswell
$number3 = 1.575 // that number needs to be rounded to 1.60
$number4 = 1.62 // rounded to 1.60
$number5 = 1.63 // to 1.65

so, I need to round it with the last digit of 0 or 5.

Thanks a lot!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Round number to closest half

Post by John Cartwright »

I don't think any of the built in rounding functions can incorporate rounding to the nearest 0 or 5. Likely, you'll have to do some custom parsing/rounding.
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: Round number to closest half

Post by internet-solution »

try

Code: Select all

number_format(($number1*2),1)/2;
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Round number to closest half

Post by VladSun »

Code: Select all

function round5($v)
{
	return round($v*2, 1)/2;
}

assert('round5(1.56) === 1.55');
assert('round5(1.574) === 1.55');
assert('round5(1.575) === 1.6');
assert('round5(1.62) === 1.6');
assert('round5(1.63) === 1.65');

echo number_format(round5(1.62), 2);
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Round number to closest half

Post by VladSun »

An universal (5*N)/(2*N) one

Code: Select all

function round5N($v, $to)
{
	$to = 1/$to;

	return round($v*$to)/$to;
}

assert('round5N(1.56, 0.05) === 1.55');
assert('round5N(1.574, 0.05) === 1.55');
assert('round5N(1.575, 0.05) === 1.6');
assert('round5N(1.62, 0.05) === 1.6');
assert('round5N(1.63, 0.05) === 1.65');

assert('round5N(1.05, 0.25) === 1.0');
assert('round5N(1.25, 0.25) === 1.25');
assert('round5N(1.35, 0.25) === 1.25');
assert('round5N(1.51, 0.25) === 1.50');
assert('round5N(1.80, 0.25) === 1.75');

assert('round5N(2, 0.5) === 2.0');
assert('round5N(2.23, 0.5) === 2.0');
assert('round5N(2.40, 0.5) === 2.5');
assert('round5N(2.55, 0.5) === 2.5');
assert('round5N(2.80, 0.5) === 3.0');

assert('round5N(2, 0.1) === 2.0');
assert('round5N(2.23, 0.1) === 2.2');
assert('round5N(2.40, 0.1) === 2.4');
assert('round5N(2.55, 0.1) === 2.6');
assert('round5N(2.80, 0.1) === 2.8');

// Good old round()
assert('round5N(2, 1) === 2.0');
assert('round5N(2.23, 1) === 2.0');
assert('round5N(2.40, 1) === 2.0');
assert('round5N(2.55, 1) === 3.0');
assert('round5N(2.80, 1) === 3.0');

assert('round5N(20, 10) === 20.0');
assert('round5N(22.3, 10) === 20.0');
assert('round5N(24.0, 10) === 20.0');
assert('round5N(25.5, 10) === 30.0');
assert('round5N(28.0, 10) === 30.0');
There are 10 types of people in this world, those who understand binary and those who don't
tonchily
Forum Commoner
Posts: 54
Joined: Thu Sep 02, 2010 10:44 am

Re: Round number to closest half

Post by tonchily »

VladSun wrote:

Code: Select all

function round5($v)
{
	return round($v*2, 1)/2;
}

assert('round5(1.56) === 1.55');
assert('round5(1.574) === 1.55');
assert('round5(1.575) === 1.6');
assert('round5(1.62) === 1.6');
assert('round5(1.63) === 1.65');

echo number_format(round5(1.62), 2);

This works great! Thanks a lot!

Just one simple question -
eg I enter value 1.58 in that function, and in return I get 1.6 . I tried rounding the same variable later with 2 decimal spots, but I can't get it to be 1.60 (I need 0 or 5 as last integer)
What am I doing wrong?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Round number to closest half

Post by VladSun »

VladSun wrote:

Code: Select all

echo number_format(round5(1.62), 2);
There are 10 types of people in this world, those who understand binary and those who don't
tonchily
Forum Commoner
Posts: 54
Joined: Thu Sep 02, 2010 10:44 am

Re: Round number to closest half

Post by tonchily »

VladSun wrote:
VladSun wrote:

Code: Select all

echo number_format(round5(1.62), 2);
Thanks a lot once again VladSun.
Post Reply