Alfred
Alfred

Reputation: 21406

How to round a fraction into next digit using PHP

I know about php's built in round() function, which return 2 for 1.6. But it return 1 for 1.1. I want to return the next number if the given number has a fractional part. For example, I want to return 2 for 1.1, or 1.01, or even 1.0001. But I don't want to return the same for 1.0, which may return 1 itself. How can I achieve this in PHP?

Upvotes: 0

Views: 202

Answers (2)

Lucky Soni
Lucky Soni

Reputation: 6888

Use ceil() to round up to the next number

echo ceil(1.0); // will echo 1

http://www.php.net/manual/en/function.ceil.php

Upvotes: 1

Aiias
Aiias

Reputation: 4748

Take a look at php's ceil().

echo ceil(4.3);    // 5
echo ceil(9.999);  // 10
echo ceil(-3.14);  // -3

Upvotes: 3

Related Questions