I want to round a number for e.g 269.00 and I round it like
round($price, 2)
which outputs 269 but when I have a value like 269.50 it ouputs 269.5 and I want to have the zero at the end this is for pricing products
$round_num = round($cost / 0.05) * 0.05; That will round whatever number you assign to the cost variable to the nearest 5 cents. This won't display zero values, so 2.20 will be displayed as 2.2 and 2.00 will be displayed a 2.
Use sprintf() Function to Show a Number to Two Decimal Places in PHP.
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.
The ceil() function is a built-in function in PHP and is used to round a number to the nearest greater integer. Parameters: The ceil() function accepts a single parameter value which represents the number which you want to round up to the nearest greater integer.
Use number_format
afterwards:
$price = number_format(round($price, 2), 2);
This also adds commas as thousands separators.
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