Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP number format without comma

Tags:

php

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?

like image 290
Chris Muench Avatar asked Jun 10 '13 16:06

Chris Muench


People also ask

How to format numbers in php?

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).

How can I set 2 decimal places in PHP?

$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.

How do you round numbers in PHP?

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.


2 Answers

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.

like image 200
bwoebi Avatar answered Oct 15 '22 12:10

bwoebi


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.

like image 45
Jason McCreary Avatar answered Oct 15 '22 10:10

Jason McCreary