Is this a decent method to display a currency number value without decimals?
$value = '100.00';
echo $value * 100/100;
Or should I be rounding incase there's a value such as '100.70'? I'm d开发者_Go百科isplaying this for a table that displays daily rates, the total value contains the digits so I assume the user won't have to really worry about decimal values...
Whether or not you should round is up to you. But for this consider using sprintf()
.
$value = '100.00';
echo sprintf("%.0f', $value);
is much better than some arithmetic hack to achieve the same thing. Also, multiplying then dividing by 100 is like multiplying by 1.
Depending on what you want to achieve exactly, number_format()
or even money_format()
may be options as well.
To do the actual rounding use round()
:
$value = '100.00';
echo round($value);
echo $value * 100/100;
is equal to $value * 1
kind sir
Use number format to format your numbers instead of doing some fancy arithmetic
Usually, people looking at columns of money want to see them all formatted the same way. With different values, the second expression would 100.01 100.1 100
instead of 100.01 100.10 100.00
.
There are several ways to format numbers, including format_number, sprintf(), and simple string manipulation.
精彩评论