derRobert
derRobert

Reputation: 571

PHP sprintf format number similar to number_format

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

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

Upvotes: 11

Views: 12451

Answers (2)

Parviz
Parviz

Reputation: 15

you can use :

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

Upvotes: -1

deceze
deceze

Reputation: 522219

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

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

Upvotes: 18

Related Questions