Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Round a number upto 2 decimal places and if the number is a whole number then add trailing zeros

Tags:

php

Question

    $number = 153.78999
    $rounded_value = round($number,2);

    $rounded_value will be 153.79

What i want is if

    $number = 153

then

    $rounded_value = 153.00

Thanks.

like image 839
user2067888 Avatar asked Dec 01 '22 18:12

user2067888


2 Answers

You can try like this:

sprintf("%0.2f",$number);

Check sprintf

or try like this:

$rounded_value = number_format($number,2);

Check number_format

like image 162
Rahul Tripathi Avatar answered May 19 '23 21:05

Rahul Tripathi


You just want the number format function $rounded_value = number_format($number,2);

like image 29
Lucas Avatar answered May 19 '23 22:05

Lucas