1

I am working to convert an excel formula to php function. Now I am facing a problem that is round up as the fraction. Please let me explain it:

  • (2 * 18)/2.98 = 12.08 but according to my requirement it should be 12.25
    • (2 * 18.5)/2.98 = 12.416 but according to my requirement it should be 12.50
    • (2 * 18.8)/2.98 = 12.617 but according to my requirement it should be 12.75

In excel, it is done like :

  • ceiling(( 2 * 18 )/2.98, 0.25) = 12.25
    • ceiling(( 2 * 18.5 )/2.98, 0.25) = 12.50
    • ceiling(( 2 * 18.8 )/2.98, 0.25) = 12.75

I tried in PHP. Ceil() and Floor() do not serve the purpose as these 2 shall give output 13 and 12 accordingly.

Another one round() uses these options PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN, PHP_ROUND_HALF_EVEN, or PHP_ROUND_HALF_ODD which does not serve the purpose.

Anyone there to help me?

ketan
  • 19,129
  • 42
  • 60
  • 98
naf4me
  • 403
  • 7
  • 17
  • 4
    This question has been answered here: http://stackoverflow.com/questions/14903379/rounding-to-nearest-fraction-half-quarter-etc – Andreas Mar 31 '16 at 04:35
  • @andreas thanks for your information. It helps also. – naf4me Mar 31 '16 at 05:06
  • @ketan what did you edit? – naf4me Mar 31 '16 at 05:46
  • @ahossain I just format your question properly. http://stackoverflow.com/posts/36324171/revisions – ketan Mar 31 '16 at 05:47
  • @ahossain is none of the answers correct? You should mark the correct answer as accepted and/or upvote. – Andreas Mar 31 '16 at 06:16
  • @andreas , yes, none the answer seems right in 100%, all they are partially correct. rather your link has given me proper solution. I used ceil() in replace of floor() for http://stackoverflow.com/questions/14903379/rounding-to-nearest-fraction-half-quarter-etc – naf4me Mar 31 '16 at 06:37

4 Answers4

1

use this function:

function fractionRound($num,$frac) {
  return ceil($num/$frac)*$frac;
}

call it this way:

echo fractionRound(( 2 * 18 )/2.98, 0.25); 

it will round to the nearest 0.25 it will return 12.25

0

PHP only provides facility to round to a precision, with the options that you found. However, you can do something similar to what you want with a little inventive mathematics and a userland function.

Try:

function roundTo($value, $roundTo = 1)
{
    $inverse = 1 / $roundTo;
    return round($value * $inverse) / $inverse;
}

See: https://ideone.com/fojMrS

Note that if you want the ceil or floor functionality you'll have to create versions of this function for ceil and floor, this is just for round.

Scopey
  • 6,269
  • 1
  • 22
  • 34
0
$YourNumber = 12.08;
$int = $YourNumber * 4;
$int = ceil($int);

echo $YourNumber ." was the start <br>";
echo $int / 4 ." is the answer";
Andreas
  • 23,610
  • 6
  • 30
  • 62
0
function roundQuarters($input)
{
    $rm = $input % 0.25;
    return $rm == 0 ? $input : $input - $rm + 0.25;
}

This will round the decimal up to the nearest 0.25, if it's also a divisor of 0.25, it'll return the same. Simple.

And you can replace 0.25 to whatever decimal you want or even parameterize that.

Taha Paksu
  • 15,371
  • 2
  • 44
  • 78