I want to make sure a float in PHP is rounded up if any decimal is present, without worrying about mathematical rounding rules. This function would work as follows:
1.1 to 2 1.2 to 2 1.9 to 2 2.3 to 3 2.8 to 3
I know the round()
function exists but I don't see any function for rounding up if any decimal is found. Is there any easy way to do this?
The round() function rounds a floating-point number. Tip: To round a number UP to the nearest integer, look at the ceil() function. Tip: To round a number DOWN to the nearest integer, look at the floor() function.
Option 1: Python Round Function Python has an inbuilt function round(), which will take the float value and round it off. The round() function takes two parameters: Number and Ndigits (optional). Ndigits represent the number of decimal places to round.
The ceil() function rounds a number UP to the nearest integer, if necessary. Tip: To round a number DOWN to the nearest integer, look at the floor() function. Tip: To round a floating-point number, look at the round() function.
You need to use the std::fixed output manipulator. (And possibly the std:setprecision() manipulator too.) float is only accurate to about 7 decimal digits not 7 decimal places. Your required output is 10 and 8 digits respectively so there will be rounding/inaccuracy.
Use the ceil
function:
$number = ceil(1.1); //2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With