Search code examples
phpdigits

Any php function to to drop decimal part if its zero


javascript Number function is handy when dropping undesired zero if decimal part is zero e.g

Number(2.00)

2

Is there such function in php or any alternative


Solution

  • I think this works that way by default in PHP. If you use proper number type like float or double.

    If you're using string then you need to map

    $a = '2.00';
    echo (float)$a; // 2
    

    Example of using float

    $a = 2.00;
    echo $a; //2
    

    or

    2.00 + 0; //2
    

    If you want to format the number to show decimal part 2.00 you need to use number_format function (http://php.net/manual/en/function.number-format.php)

    $a = 2.00
    echo number_format($a, 2); // 2.00