Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save developerinusa/e9f509b1cd3c7c29d812a7ebe774d87f to your computer and use it in GitHub Desktop.
Save developerinusa/e9f509b1cd3c7c29d812a7ebe774d87f to your computer and use it in GitHub Desktop.
Fiyat gösteriminde son haneyi sürekli 5 ya da 0 olacak şekilde yukarı yuvarlıyor. // Rounds fraction of a number's last digit multiples of 5
<?php
/**
* @param $sayi
* @param string $seperator
* @return float|int
*
* Fiyat gösteriminde son haneyi sürekli 5 ya da 0 olacak şekilde yukarı yuvarlıyor.
* 99.99 => 100
* 10.02 => 10.05
* 0.1 => 0.5
*/
function roundPriceToMultiplesOfFive($number, $seperator=".")
{
$roundedNumber = $number;
$parsedNumber = explode($seperator,$number);
if($parsedNumber[1]){
$fractionLength = strlen($parsedNumber[1]);
$fractionLastDigit = substr($parsedNumber[1],-1);
if(in_array($fractionLastDigit,[0,5]))
return number_format($roundedNumber,2,".","");
$x = (10 - $fractionLastDigit);
$y = ( 5 - $fractionLastDigit);
$addToNumber = $x < 5 ? $x : abs($y);
$roundedNumber = $number + ($addToNumber / pow(10,$fractionLength));
}
return number_format($roundedNumber,2,".","");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment