0

issue with converting number of product to dozen.

I have 40 products and I want to convert it into dozen.

when I use this calculation 40 / 12 = 3.3333333333333335 and number_format to 1 decimal it returns 3.3 but it means 39 products

so I want that if number after decimal are more than 2 i.e 3.33333 then it round to greater number after decimal that is 3.4

How can I convert that?

Paritosh Mahale
  • 1,238
  • 2
  • 14
  • 42
  • This may be useful: https://stackoverflow.com/questions/17564365/how-to-round-up-value-in-php – Sercan Jun 15 '20 at 17:21
  • Are you looking to always round up? If so: `echo ceil(4.3); // 5` – Rob Moll Jun 15 '20 at 17:23
  • 1
    Does this answer your question? [How can I make sure a float will always be rounded up with PHP?](https://stackoverflow.com/questions/5093608/how-can-i-make-sure-a-float-will-always-be-rounded-up-with-php) – Viet Dinh Jun 15 '20 at 17:29

1 Answers1

1

We can custom function round_up like:

function round_up($number, $precision = 2)
{
    $fig = pow(10, $precision);
    return (ceil($number * $fig) / $fig);
}

var_dump(round_up(3.3333333, 2));
Viet Dinh
  • 1,871
  • 1
  • 6
  • 18