Re: [opensuse] php printf with thousands separator?
Op vrijdag 7 juli 2017 15:19:55 CEST schreef Knurpht - Gertjan Lettink:
Op vrijdag 7 juli 2017 13:14:35 CEST schreef Per Jessen:
Knurpht - Gertjan Lettink wrote:
Op vrijdag 7 juli 2017 11:51:33 CEST schreef Per Jessen:
Per Jessen wrote:
Does anyone know why PHP printf() does not (apparently) support the ' (single quote) to indicate a number format with thousands separator?
This is the printf() format (for instance): "%'.2f". This works fine in C and with bash:printf, but somehow not in php. I was just wondering why that might be?
No idea why, but I mostly use number_format(..), see http://php.net/manual/en/function.number-format.php
Yeah, that's my googling kept tellme too. That won't work though - the patterns are given to me as fields from a 3rd party. They have to work with printf(), that is the only limitation. When I call printf() in php, I expect(ed) it to work with all the patterns from from C. If I were to use number_format(), it won't work with "%.2f".
I can't see the input, but wouldn't number_format(printf("%.2f)") do the job? That should have been number_format(printf("%.2f"))
-- Gertjan Lettink, a.k.a. Knurpht openSUSE Board Member openSUSE Forums Team -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Knurpht - Gertjan Lettink wrote:
Op vrijdag 7 juli 2017 15:19:55 CEST schreef Knurpht - Gertjan Lettink:
Op vrijdag 7 juli 2017 13:14:35 CEST schreef Per Jessen:
Knurpht - Gertjan Lettink wrote:
Op vrijdag 7 juli 2017 11:51:33 CEST schreef Per Jessen:
Per Jessen wrote:
Does anyone know why PHP printf() does not (apparently) support the ' (single quote) to indicate a number format with thousands separator?
This is the printf() format (for instance): "%'.2f". This works fine in C and with bash:printf, but somehow not in php. I was just wondering why that might be?
No idea why, but I mostly use number_format(..), see http://php.net/manual/en/function.number-format.php
Yeah, that's my googling kept tellme too. That won't work though - the patterns are given to me as fields from a 3rd party. They have to work with printf(), that is the only limitation. When I call printf() in php, I expect(ed) it to work with all the patterns from from C. If I were to use number_format(), it won't work with "%.2f".
I can't see the input, but wouldn't number_format(printf("%.2f)") do the job? That should have been number_format(printf("%.2f"))
Yes, I think it would, but what happens when the field-spec is a string or a hex format (for instance). Or when the format is "%'.2f". You have to imagine a call like this: dosomething( data, format ); 'data' is whatever, 'format' is a printf() formatting specification. That's what I have to deal with. I could write some concoction that would parse the input spec and use number_format() or printf(), but I would much prefer not having to parse that formatting spec. -- Per Jessen, Zürich (31.1°C) http://www.hostsuisse.com/ - dedicated server rental in Switzerland. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Op vrijdag 7 juli 2017 15:49:57 CEST schreef Per Jessen:
Knurpht - Gertjan Lettink wrote:
Op vrijdag 7 juli 2017 15:19:55 CEST schreef Knurpht - Gertjan
Lettink:
Op vrijdag 7 juli 2017 13:14:35 CEST schreef Per Jessen:
Knurpht - Gertjan Lettink wrote:
Op vrijdag 7 juli 2017 11:51:33 CEST schreef Per Jessen:
Per Jessen wrote: > Does anyone know why PHP printf() does not (apparently) > support the ' (single quote) to indicate a number format with > thousands separator?
This is the printf() format (for instance): "%'.2f". This works fine in C and with bash:printf, but somehow not in php. I was just wondering why that might be?
No idea why, but I mostly use number_format(..), see http://php.net/manual/en/function.number-format.php
Yeah, that's my googling kept tellme too. That won't work though - the patterns are given to me as fields from a 3rd party. They have to work with printf(), that is the only limitation. When I call printf() in php, I expect(ed) it to work with all the patterns from from C. If I were to use number_format(), it won't work with "%.2f".
I can't see the input, but wouldn't number_format(printf("%.2f)") do the job?
That should have been number_format(printf("%.2f"))
Yes, I think it would, but what happens when the field-spec is a string or a hex format (for instance). Or when the format is "%'.2f".
You have to imagine a call like this:
dosomething( data, format );
'data' is whatever, 'format' is a printf() formatting specification.
That's what I have to deal with. I could write some concoction that would parse the input spec and use number_format() or printf(), but I would much prefer not having to parse that formatting spec.
Well, I did some more reading and only found that printf alone cannot doe what you want to achieve. I've been struggling with this in the past where I had to deal with something like this (get data from one system into another). Never found another way then nestiing commands like described. -- Gertjan Lettink, a.k.a. Knurpht openSUSE Board Member openSUSE Forums Team -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Knurpht - Gertjan Lettink wrote:
Op vrijdag 7 juli 2017 15:49:57 CEST schreef Per Jessen:
Knurpht - Gertjan Lettink wrote:
Op vrijdag 7 juli 2017 15:19:55 CEST schreef Knurpht - Gertjan
I can't see the input, but wouldn't number_format(printf("%.2f)") do the job?
That should have been number_format(printf("%.2f"))
Yes, I think it would, but what happens when the field-spec is a string or a hex format (for instance). Or when the format is "%'.2f".
You have to imagine a call like this:
dosomething( data, format );
'data' is whatever, 'format' is a printf() formatting specification.
That's what I have to deal with. I could write some concoction that would parse the input spec and use number_format() or printf(), but I would much prefer not having to parse that formatting spec.
Well, I did some more reading and only found that printf alone cannot do what you want to achieve.
Right. I was just curious if anyone happened to know why php's printf() doesn't support the thousands separator.
I've been struggling with this in the past where I had to deal with something like this (get data from one system into another). Never found another way then nestiing commands like described.
Except it only works when the pattern is known beforehand. I also wonder if number_format() takes the locale into account. (decimal pt and thousands separator). -- Per Jessen, Zürich (31.4°C) http://www.hostsuisse.com/ - dedicated server rental in Switzerland. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On Fri, 07 Jul 2017 16:32:32 +0200 Per Jessen <per@computer.org> wrote:
Knurpht - Gertjan Lettink wrote:
Op vrijdag 7 juli 2017 15:49:57 CEST schreef Per Jessen:
Knurpht - Gertjan Lettink wrote:
Op vrijdag 7 juli 2017 15:19:55 CEST schreef Knurpht - Gertjan
I can't see the input, but wouldn't number_format(printf("%.2f)") do the job?
That should have been number_format(printf("%.2f"))
Yes, I think it would, but what happens when the field-spec is a string or a hex format (for instance). Or when the format is "%'.2f".
You have to imagine a call like this:
dosomething( data, format );
'data' is whatever, 'format' is a printf() formatting specification.
That's what I have to deal with. I could write some concoction that would parse the input spec and use number_format() or printf(), but I would much prefer not having to parse that formatting spec.
Well, I did some more reading and only found that printf alone cannot do what you want to achieve.
Right. I was just curious if anyone happened to know why php's printf() doesn't support the thousands separator.
I've been struggling with this in the past where I had to deal with something like this (get data from one system into another). Never found another way then nestiing commands like described.
Except it only works when the pattern is known beforehand. I also wonder if number_format() takes the locale into account. (decimal pt and thousands separator).
You do know that the thousands separator does depend on the locale being set to a specific value, right? Maybe PHP is just slow at catching up with a new C feature? Or something in the build chain is? (I have no idea how PHP actually implements things - I spit in its face, unfortunately). -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Op zaterdag 8 juli 2017 01:00:15 CEST schreef Dave Howorth:
On Fri, 07 Jul 2017 16:32:32 +0200
Per Jessen <per@computer.org> wrote:
Knurpht - Gertjan Lettink wrote:
Op vrijdag 7 juli 2017 15:49:57 CEST schreef Per Jessen:
Knurpht - Gertjan Lettink wrote:
Op vrijdag 7 juli 2017 15:19:55 CEST schreef Knurpht - Gertjan
I can't see the input, but wouldn't number_format(printf("%.2f)") do the job?
That should have been number_format(printf("%.2f"))
Yes, I think it would, but what happens when the field-spec is a string or a hex format (for instance). Or when the format is "%'.2f".
You have to imagine a call like this:
dosomething( data, format );
'data' is whatever, 'format' is a printf() formatting specification.
That's what I have to deal with. I could write some concoction that would parse the input spec and use number_format() or printf(), but I would much prefer not having to parse that formatting spec.
Well, I did some more reading and only found that printf alone cannot do what you want to achieve.
Right. I was just curious if anyone happened to know why php's printf() doesn't support the thousands separator.
I've been struggling with this in the past where I had to deal with something like this (get data from one system into another). Never found another way then nestiing commands like described.
Except it only works when the pattern is known beforehand. I also wonder if number_format() takes the locale into account. (decimal pt and thousands separator).
You do know that the thousands separator does depend on the locale being set to a specific value, right?
Maybe PHP is just slow at catching up with a new C feature? Or something in the build chain is? (I have no idea how PHP actually implements things - I spit in its face, unfortunately).
http://php.net/manual/en/class.numberformatter.php -- Gertjan Lettink, a.k.a. Knurpht openSUSE Board Member openSUSE Forums Team -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Dave Howorth wrote:
On Fri, 07 Jul 2017 16:32:32 +0200 Per Jessen <per@computer.org> wrote:
Knurpht - Gertjan Lettink wrote:
Op vrijdag 7 juli 2017 15:49:57 CEST schreef Per Jessen:
Knurpht - Gertjan Lettink wrote:
Op vrijdag 7 juli 2017 15:19:55 CEST schreef Knurpht - Gertjan
I can't see the input, but wouldn't number_format(printf("%.2f)") do the job?
That should have been number_format(printf("%.2f"))
Yes, I think it would, but what happens when the field-spec is a string or a hex format (for instance). Or when the format is "%'.2f".
You have to imagine a call like this:
dosomething( data, format );
'data' is whatever, 'format' is a printf() formatting specification.
That's what I have to deal with. I could write some concoction that would parse the input spec and use number_format() or printf(), but I would much prefer not having to parse that formatting spec.
Well, I did some more reading and only found that printf alone cannot do what you want to achieve.
Right. I was just curious if anyone happened to know why php's printf() doesn't support the thousands separator.
I've been struggling with this in the past where I had to deal with something like this (get data from one system into another). Never found another way then nestiing commands like described.
Except it only works when the pattern is known beforehand. I also wonder if number_format() takes the locale into account. (decimal pt and thousands separator).
You do know that the thousands separator does depend on the locale being set to a specific value, right?
Yup. When we process this stuff, we use en_GB.utf8, fr_CH.utf8, it_CH.utf8 or de_CH.utf8.
Maybe PHP is just slow at catching up with a new C feature?
Is it new? Hmm, I see it in the printf man page on my openSUSE 10.3 system, it's not overly new.
Or something in the build chain is? (I have no idea how PHP actually implements things - I spit in its face, unfortunately).
In PHP, I have always thought these C library functions were more or less implemented as "adapt the arguments, call the C function", but that's clearly not always the case. I guess a quick glance at the source will explain. -- Per Jessen, Zürich (24.7°C) http://www.hostsuisse.com/ - virtual servers, made in Switzerland. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
participants (3)
-
Dave Howorth
-
Knurpht - Gertjan Lettink
-
Per Jessen