Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php round number include zero if .50

Tags:

php

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

like image 660
ONYX Avatar asked May 08 '13 05:05

ONYX


People also ask

How do I round to the nearest .5 in PHP?

$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.

How can I get 2 decimal places in PHP?

Use sprintf() Function to Show a Number to Two Decimal Places in PHP.

How do you round off decimals 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.

How do you round to the nearest whole number in PHP?

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.


1 Answers

Use number_format afterwards:

$price = number_format(round($price, 2), 2);

This also adds commas as thousands separators.

like image 83
icktoofay Avatar answered Oct 28 '22 19:10

icktoofay