PHP number: decimal point visible only if needed

后端 未结 12 1586
死守一世寂寞
死守一世寂寞 2021-01-31 06:48

I\'d like to know if exists some function to automatically format a number by it\'s decimal, so if I have:



        
12条回答
  •  离开以前
    2021-01-31 07:41

    floatval or simply casting to float

    php > echo floatval(7.00);
    7
    php > echo floatval(2.30);
    2.3
    php > echo floatval(1.25);
    1.25
    php > echo floatval(1.125);
    1.125
    
    php > echo (float) 7.00;
    7
    php > echo (float) 2.30;
    2.3
    php > echo (float) 1.25;
    1.25
    php > echo (float) 1.125;
    1.125
    

提交回复
热议问题