Round large non decimal numbers, up and down with php

How to combine large numbers in php.

NOTE. I tried the round function and cannot make it work as I need

eg:

Say I have 4 listings in a database and they have 4 different prices.

    1st Price = 5,783

    2nd Price = 19,647

    3rd Price = 12,867

    4th Price = 23,647


Now we determine that the lowest price in the databases will be 5,783

and the highest price is 23.647 .


now what i want to do is round the lowest price to say the nearest 500 or 1000 or even 5000


approximate example 1000

lowest price 5,783 rounded = 5,000

23,647 = 24000

+3
2

:

function nearest($num, $divisor) {
  $diff = $num % $divisor;
  if ($diff == 0)
    return $num;
  elseif ($diff >= ceil($divisor / 2))
    return $num - $diff + $divisor;
  else
    return $num - $diff;
}

:

nearest(23647, 5000);

, , :

function roundUp($num, $divisor) {
  $diff = $num % $divisor;
  if ($diff == 0)
    return $num;
  else
    return $num - $diff + $divisor;
}


function roundDown($num, $divisor) {
  $diff = $num % $divisor;
  return $num - $diff;
}
+7

php manual. :

<?php
    echo round(3.4);         // 3
    echo round(3.5);         // 4
    echo round(3.6);         // 4
    echo round(3.6, 0);      // 4
    echo round(1.95583, 2);  // 1.96
    echo round(1241757, -3); // 1242000
    echo round(5.045, 2);    // 5.05
    echo round(5.055, 2);    // 5.06
?>

,

round(23647, -3) 

, (. ).

',', number_format

+2

All Articles