How to add .00 in any value using PHP?

前端 未结 5 1439
情书的邮戳
情书的邮戳 2021-01-18 11:48

I want to add .00 to my value.

For example:

100 will be 100.00
100.26 will be 100.26 only.

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-18 12:10

    Like @Gaurav said, use the number_format() function. Simply pass it the value and the number of digits you want there to be after the decimal point:

    $value = 100;
    echo number_format($value, 2); //prints "100.00"
    

    Note that by default, it will also insert commas as the thousands separator:

    $value = 2013;
    echo number_format($value, 2); //prints "2,013.00"
    

    You can change the characters that are used as the decimal point and thousands separator by passing them in as the third and fourth parameters to the function:

    $value = 2013;
    echo number_format($value, 2, ',', ' '); //prints "2 013,00"
    

提交回复
热议问题