Home » Php » PHP sprintf format number similar to number_format

PHP sprintf format number similar to number_format

Posted by: admin July 12, 2020 Leave a comment

Questions:

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

How to&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.

Answer:

you can use :

sprintf("%.2f", $val);