## How to ceil, floor and round bcmath numbers?

After a night lost trying to solve this problem I believe I’ve found a rather simple solution, here it is: function bcceil(\$number) { if (strpos(\$number, ‘.’) !== false) { if (preg_match(“~\.[0]+\$~”, \$number)) { return bcround(\$number, 0); } if (\$number[0] != ‘-‘) { return bcadd(\$number, 1, 0); } return bcsub(\$number, 0, 0); } return \$number; } … Read more

## 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 … Read more

## Round minute down to nearest quarter hour

\$seconds = time(); \$rounded_seconds = round(\$seconds / (15 * 60)) * (15 * 60); echo “Original: ” . date(‘H:i’, \$seconds) . “\n”; echo “Rounded: ” . date(‘H:i’, \$rounded_seconds) . “\n”; This example gets the current time and rounds it to the nearest quarter and prints both the original and the rounded time. PS: If you … Read more

## How does x|0 floor the number in JavaScript?

Because, according to the ECMAScript specifications, bitwise operators operators call ToInt32 on each expression to be evaluated. See 11.10 Binary Bitwise Operators: The production A : A @B, where @ is one of the bitwise operators in the productions above, is evaluated as follows: Evaluate A. Call GetValue(Result(1)). Evaluate B. Call GetValue(Result(3)). Call ToInt32(Result(2)). Call … Read more