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
Use sprintf() Function to Show a Number to Two Decimal Places in 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 )
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".
sprintf('A number: %s', number_format(1599, 0, '.', ','))
No, there is no other way. (s)printf
doesn't have options for adding thousand separators.
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