Page 1 of 1
Round number to closest half
Posted: Sun Oct 03, 2010 3:11 pm
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!
Re: Round number to closest half
Posted: Sun Oct 03, 2010 5:14 pm
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.
Re: Round number to closest half
Posted: Mon Oct 04, 2010 5:53 am
by internet-solution
Re: Round number to closest half
Posted: Mon Oct 04, 2010 6:07 am
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);
Re: Round number to closest half
Posted: Mon Oct 04, 2010 6:39 am
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');
Re: Round number to closest half
Posted: Thu Oct 07, 2010 5:03 am
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?
Re: Round number to closest half
Posted: Thu Oct 07, 2010 5:10 am
by VladSun
VladSun wrote:Code: Select all
echo number_format(round5(1.62), 2);
Re: Round number to closest half
Posted: Thu Oct 07, 2010 5:15 am
by tonchily
VladSun wrote:VladSun wrote:Code: Select all
echo number_format(round5(1.62), 2);
Thanks a lot once again VladSun.