1

I have the integer

$price = 19.999999995;

I want the php to print out any integers similar to $price integer to upper integer value which is "20".

$total = ceil($price);

I am expecting the above $total value to be 20 , but it's return "21".

How can i make that ? and why it's returning 21 instead of 20 .

Q8root
  • 1,243
  • 3
  • 25
  • 48

1 Answers1

2

Use the round() function.

$price = 19.999999995;
$total = round($price);
echo $total;

As stated in the PHP documentation:

Note that 'ceil' can show unexpected behaviour when using float values due to inaccuracies in the float system and PHP tendency to freely limiting the number of digits it displays to the user.

Matt Kent
  • 1,145
  • 1
  • 11
  • 26