-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
34 lines (27 loc) · 874 Bytes
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?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,".","");
}