Created
May 3, 2021 10:29
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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