Skip to content

Instantly share code, notes, and snippets.

@msamgan
Last active July 26, 2022 19:15
Show Gist options
  • Save msamgan/c89ddb034761602bfc7a1f402151cc6b to your computer and use it in GitHub Desktop.
Save msamgan/c89ddb034761602bfc7a1f402151cc6b to your computer and use it in GitHub Desktop.
<?php
/**
* * Problem Statement:
* * passing a floating point number to a function, the function will round the number to
* * the nearest point five and return the result.
*
* * Example:
* * roundUpToNextFive(3.7) // returns 4.0
* * roundUpToNextFive(3.3) // returns 3.5
* * roundUpToNextFive(3.8) // returns 4.0
* * roundUpToNextFive(1.2) // returns 1.5
*/
/**
* Rounds the given number up to the next five.
*
* @param float $number
* @return float
*/
function roundUpToNextFive(float $number)
{
printf("The number is %.2f \n", $number);
$decimalPart = $number - floor($number);
return ($decimalPart > 0.50 ? ceil($decimalPart) : (ceil($decimalPart) / 2)) + floor($number);
}
printf("The next closest five is %.2f \n", roundUpToNextFive(5.33));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment