Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP sprintf format number similar to number_format

How can I format floats by sprintf like I would do by number_format()? I need

  • no decimals
  • a dot as thousand separator

With number_format() I would do so

$number = number_format(1599, 0, ".", ",");

The result should be:

1599 => 1.500
899.99 => 899
70 => 70

Is this possible using sprintf()?

Kind regards, Robert

like image 644
derRobert Avatar asked May 17 '13 07:05

derRobert


People also ask

How can I get 2 decimal places in PHP?

Use sprintf() Function to Show a Number to Two Decimal Places in PHP.

What is number format PHP?

The number_format() function is an inbuilt function in PHP which is used to format a number with grouped thousands. It returns the formatted number on success otherwise it gives E_WARNING on failure. Syntax: string number_format ( $number, $decimals, $decimalpoint, $sep )

What does sprintf () function do in PHP?

The sprintf() function writes a formatted string to a variable. The arg1, arg2, ++ parameters will be inserted at percent (%) signs in the main string. This function works "step-by-step".


1 Answers

sprintf('A number: %s', number_format(1599, 0, '.', ','))

No, there is no other way. (s)printf doesn't have options for adding thousand separators.

like image 153
deceze Avatar answered Nov 13 '22 02:11

deceze