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

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