How to add .00 in any value using PHP?

前端 未结 5 1440
情书的邮戳
情书的邮戳 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 11:55

    number_format() can be your friend

    0 讨论(0)
  • 2021-01-18 12:02

    you can use

    round()

    or

    number_format()

    0 讨论(0)
  • 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"
    
    0 讨论(0)
  • 2021-01-18 12:10
    number_format(100, 2, '.', ' ')
    
    0 讨论(0)
  • 2021-01-18 12:11
    $YOUR_VALUE = 1000.25;
    echo number_format($YOUR_VALUE, 2); 
    
    0 讨论(0)
提交回复
热议问题