简体   繁体   中英

PHP precision rounding question

Wanted to as a simple question.

Code:

$myarray = array(5.0000);
echo round($myarray[0],2);

Ouput:

5

Why is the output 5 and not 5.00 ?

Thanks

It's because round actually rounds off the data you give to it, it doesn't change how that data is printed.

If you want to specify how the data is output, look into using printf , such as:

printf ("%.2f\n", $myarray[0]);

It doesn't show the 0's after the decimal point if there are only 0's.

5.050 would be shown as 5.05...

you can have a look at the number_format function if you want to show the 0's.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM