In PHP, how to print a number with 2 decimals, but only if there are decimals already?

前端 未结 6 1003
抹茶落季
抹茶落季 2021-01-14 08:33

I have a basic index.php page with some variables that I want to print in several places - here are the variables:



        
6条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-14 09:17

    You could use

       if (is_float($var)) 
       {
         echo number_format($var,2,'.','');
       }
       else
       {
         echo $var;
       }
    

提交回复
热议问题