I want to display the number 1000.5 like 1000.50 with 2 decimal places and no commas/thousands separators.
I am using number_format to achieve this:
number_format(1000.5, 2);
This results 1,000.50. The comma (,) separator appended in thousand place which is not required in the result.
How can I display the number with a trailing zero and no comma?
PHP's number_format() function gives you an easy way to format numbers for displaying to the user. You can separate thousands with commas or other separators, choose the decimal point character, and choose the number of decimal places to display (or display no decimals at all).
$twoDecNum = sprintf('%0.2f', round($number, 2)); The rounding correctly rounds the number and the sprintf forces it to 2 decimal places if it happens to to be only 1 decimal place after rounding. Show activity on this post. This will give you 2 number after decimal.
The round() function rounds a floating-point number. Tip: To round a number UP to the nearest integer, look at the ceil() function. Tip: To round a number DOWN to the nearest integer, look at the floor() function.
See the documentation for number_format: http://php.net/number_format
The functions parameters are:
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
So use:
number_format(1000.5, 2, '.', '');
Which means that you don't use any (= empty string) thousands separator, only a decimal point.
number_format()
takes additional parameters:
number_format(1000.5, 2, '.', '');
The default is a period (.
) for the decimal separator and a comma (,
) for the thousands separator. I'd encourage you to read the documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With