Welcome! Log In Create A New Profile

Advanced

[PHP] Any reason why printf() does not support formats with thousands separator?

Posted by Per Jessen 
$SUBJ says it all - I've recently discovered that a format such
as %'.2f" results in this:

php -r "printf(\"%'.2f\", 23232323.237);"
23232323.237000

I know that number_format() will do the same, but I am given the format
spec as an interface, I would need to try to parse it and then decide
whether to use printf() or number_format().


--
Per Jessen, Zürich (19.9°C)
http://www.hostsuisse.com/ - virtual servers, made in Switzerland.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
On Monday 10 July 2017 03:07:50 Per Jessen wrote:
> $SUBJ says it all - I've recently discovered that a format such
> as %'.2f" results in this:
>
> php -r "printf(\"%'.2f\", 23232323.237);"
> 23232323.237000
>
> I know that number_format() will do the same, but I am given the format
> spec as an interface, I would need to try to parse it and then decide
> whether to use printf() or number_format().

Try

php -r "printf(\"%.2f\", 232323.4567);"
232323.46

John

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
John Iliffe wrote:
> On Monday 10 July 2017 03:07:50 Per Jessen wrote:
>> $SUBJ says it all - I've recently discovered that a format such
>> as %'.2f" results in this:
>>
>> php -r "printf(\"%'.2f\", 23232323.237);"
>> 23232323.237000
>>
>> I know that number_format() will do the same, but I am given the format
>> spec as an interface, I would need to try to parse it and then decide
>> whether to use printf() or number_format().
>
> Try
>
> php -r "printf(\"%.2f\", 232323.4567);"
> 232323.46

That works, but doesn't do what I want - add thousands separators :-)



/Per

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
On Mon, Jul 10, 2017 at 8:40 AM, Per Jessen <[email protected]> wrote:

> John Iliffe wrote:
>
>> On Monday 10 July 2017 03:07:50 Per Jessen wrote:
>>
>>> $SUBJ says it all - I've recently discovered that a format such
>>> as %'.2f" results in this:
>>>
>>> php -r "printf(\"%'.2f\", 23232323.237);"
>>> 23232323.237000
>>>
>>> I know that number_format() will do the same, but I am given the format
>>> spec as an interface, I would need to try to parse it and then decide
>>> whether to use printf() or number_format().
>>>
>>
>> Try
>> php -r "printf(\"%.2f\", 232323.4567);"
>> 232323.46
>>
>
> That works, but doesn't do what I want - add thousands separators :-)
>
>
From the manual [1]: "An alternate padding character can be specified by
prefixing it with a single quote (')." and doesn't mention using it for
thousands like the C printf does.

[1] - http://php.net/manual/en/function.sprintf.php


>
>
> /Per
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Ryan Pallas wrote:

> On Mon, Jul 10, 2017 at 8:40 AM, Per Jessen <[email protected]> wrote:
>
>> John Iliffe wrote:
>>
>>> On Monday 10 July 2017 03:07:50 Per Jessen wrote:
>>>
>>>> $SUBJ says it all - I've recently discovered that a format such
>>>> as %'.2f" results in this:
>>>>
>>>> php -r "printf(\"%'.2f\", 23232323.237);"
>>>> 23232323.237000
>>>>
>>>> I know that number_format() will do the same, but I am given the
>>>> format spec as an interface, I would need to try to parse it and
>>>> then decide whether to use printf() or number_format().
>>>>
>>>
>>> Try
>>> php -r "printf(\"%.2f\", 232323.4567);"
>>> 232323.46
>>>
>>
>> That works, but doesn't do what I want - add thousands separators :-)
>>
>>
> From the manual [1]: "An alternate padding character can be specified
> by prefixing it with a single quote (')." and doesn't mention using it
> for thousands like the C printf does.

I had not actually read that bit, but I had noticed that the thousands
separator character was not mentioned (as opposed to the C version).

It just seems such a really odd omission, so I got curious as to why. I
wonder if anyone's written a fully C-compliant printf() for php?



--
Per Jessen, Zürich (23.9°C)
http://www.hostsuisse.com/ - dedicated server rental in Switzerland.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login