0

I am using a function to make 1000 to 1k and 1000000 to 1m but i want to add one decimal to it, i have gotten this far but it rounds up instead of down so if i enter 1565 i want to get 1.5 and only if i pass 1600 i want to get 1.6 (force round down) i use this function:

public function convert($Input){
    if($Input<1000){
        $AmountCode = "";
        $Amount = $Input;
    }
    else if($Input>=1000000){
        $AmountCode = "M";
        $Amount = round(floatval($Input / 1000000), 1, PHP_ROUND_HALF_DOWN);
    }
    else if($Input>=1000){
        $AmountCode = "K";
        $Amount = round($Input / 1000, 1, PHP_ROUND_HALF_DOWN);

    }

    $Array = array(
        'Amount' => $Amount,
        'Code' => $AmountCode
    );

    return $Array;

}

Scenario 1: what happens now if i enter 1565 i get 1.6 and if i enter 1545 i get 1.5 does any one know how to get the forcibly round it down?

Scenario 2: the number i enter is 10665 will be outputted as 10.7k (rounded up), but i want to to show as 10.6k (rounded down) which for somereason i can't figure out how to do that

Timo
  • 87
  • 3
  • 13
  • see my previous answer http://stackoverflow.com/a/32624297/4098311 – Halayem Anis Feb 26 '16 at 14:37
  • 1
    Possible duplicate of [Convert amounts where input is "100K", "100M" etc](http://stackoverflow.com/questions/32623315/convert-amounts-where-input-is-100k-100m-etc) – Halayem Anis Feb 26 '16 at 14:38
  • @HalayemAnis That is not exactly what i mean i want if a number is 10665 i want it to become 10.6K now i got that part but what it is doing its rounding up so 10665 will actually become 10.7K instead of the rounding down – Timo Feb 26 '16 at 14:41

1 Answers1

1

For rounding down in PHP, they have a function called floor(). Unfortunatly, this function does only return an int. However you can make it round down by multiplication first, and then division. See this post: PHP How do I round down to two decimal places?.

This means your code would be something like this:

else if($Input>=1000000){
    $AmountCode = "M";
    $Amount = floor(floatval($Input / 100000))/10;
}
else if($Input>=1000){
    $AmountCode = "K";
    $Amount = floor(floatval($Input / 100))/10;

}
Community
  • 1
  • 1