如何使用PHP在任何值中添加.00?

Kan*_*ela 6 php

我想为我的价值添加.00.

例如:

100将是100.00
100.26将仅为100.26.

小智 11

$YOUR_VALUE = 1000.25;
echo number_format($YOUR_VALUE, 2); 
Run Code Online (Sandbox Code Playgroud)


Gau*_*rav 10

number_format()可以成为你的朋友


Mic*_*ael 6

就像@Gaurav所说的那样,使用该number_format()函数。只需将小数点后的值和位数传递给它即可:

$value = 100;
echo number_format($value, 2); //prints "100.00"
Run Code Online (Sandbox Code Playgroud)

请注意,默认情况下,它还会插入逗号作为千位分隔符:

$value = 2013;
echo number_format($value, 2); //prints "2,013.00"
Run Code Online (Sandbox Code Playgroud)

您可以通过将它们作为第三个和第四个参数传递给函数来更改用作小数点和千位分隔符的字符:

$value = 2013;
echo number_format($value, 2, ',', ' '); //prints "2 013,00"
Run Code Online (Sandbox Code Playgroud)