Rounding to Nearest Fraction (Half, Quarter, etc.)

Rounding to nearest fraction (half, quarter, etc.)

Since you're looking for fourths (.00, .25, .50, .75), multiply your number by 4, round to nearest whole number as desired (floor if down, ceil if up), then divide by 4.

1.32, down to nearest fourth:

1.32 * 4 = 5.28

floor(5.28) = 5.00

5.00 / 4 = 1.25

Same principle applies for any other fractions, such as thirds or eighths (.0, .125, .25, .375, .5, .625, .75, .875). For example:

1.77, up to nearest eighth:

1.77 * 8 = 14.16

ceil(14.16) = 15.00

15.00 / 8 = 1.875


Just for fun, you could write a function like this:

function floorToFraction($number, $denominator = 1)
{
$x = $number * $denominator;
$x = floor($x);
$x = $x / $denominator;
return $x;
}

echo floorToFraction(1.82); // 1
echo floorToFraction(1.82, 2); // 1.5
echo floorToFraction(1.82, 3); // 1.6666666666667
echo floorToFraction(1.82, 4); // 1.75
echo floorToFraction(1.82, 9); // 1.7777777777778
echo floorToFraction(1.82, 25); // 1.8

Rounding function to nearest 1/4

0.25 is 1/4, so you can get it easily by

double rounded = round(4.0 * hoursEntered) / 4.0

round does not exist. You must use appropriate rounding function and options. For aspx.cs (C# codebehind) see System.Math.Round.

Also, to link it to the textbox on the webpage, you will need some rule/validator or textchange handler or anywhere around the typical text parsing. You may also try doing it in JavaScript client side, the *4/4 trick stays the same, just round function will have different name.

Round up decimal number according to fraction in PHP

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

How do I round to the nearest 0.5?

Multiply your rating by 2, then round using Math.Round(rating, MidpointRounding.AwayFromZero), then divide that value by 2.

Math.Round(value * 2, MidpointRounding.AwayFromZero) / 2

Javascript: Math.round() per 0.25?

Just multiply the number by 4, round it, and divide by 4:

[2.1, 2.2, 3.4, 5.6, 9.8, 9.8, 9.9].forEach(  num => console.log(Math.round(num * 4) / 4));

How to round half up

Here is one option. We can multiply the input number by 2, then take the ceiling, and finally divide by two, e.g.

$input = 1.16;
$input_rounded = ceil(2*$input) / 2;
echo $input_rounded;

Floor number to nearest quarter, to three decimal places?

.125 isn't the nearest quarter, it's the nearest eighth.

Your example is correct, if you want the nearest quarter, it's 145.5. If you want the nearest eighth, that would be 145.625:

>>> math.floor(lon * 8) / 8
145.625

or in PHP:

$longitude = 145.6360003;
echo floor($longitude * 8) / 8;

You should also consider having round($result, 3) as the last step because of the inherent inaccuracy of floating point numbers.

Round second digit after decimal to produce nice number

If I understand your desired output correctly, that you only want to round the second decimal point, you can round with 1 decimal presicion, then use numer_format() to ensure you get the correct number of decimals.

$num = 30.61;
echo number_format(round($num, 1), 2);
  • round() documentation
  • number_format() documentation
  • Live demo


Related Topics



Leave a reply



Submit