Hello.
How would I format a number using sprintf()? I need a number like 1000000 to be formated like 1,000,000 or preferably $1,000,000.
The format should also work on smaller or even larger numbers, up to 999,999,999.
Thanks!
Hello.
How would I format a number using sprintf()? I need a number like 1000000 to be formated like 1,000,000 or preferably $1,000,000.
The format should also work on smaller or even larger numbers, up to 999,999,999.
Thanks!
Check out [man]number_format[/man] instead. You might still be able to do this with sprintf, but you would need supporting code. Number_format instead will do all the work except toss the dollar sign on there. And that shoudl be easy: echo '$' . number_format($value);
I have to use sprintf(). I am using a 3rd party program that is expecting a sprintf() format.
There's no fixed format string to do that; sprintf's format syntax doesn't go as far as "split a string into groups of three characters and insert commas between groups".
Is this third party function something that you send the format string and the data to display in that format every time you have something to be displayed, or is providing that format some sort of setting, that is later used over and over? If the former, then you can format the number yourself and provide that to the function as the "format string" - literal strings are sprintf() format strings, too (with the only difference being that % has to be written as %%).
Well it's like this....
The Parmater is
SERIE_DATA_1=12345|12345|12345|12345|etc...
Now, as long as I enter integers everything is fine, but when I try to enter numbers that are formated such as 12,345 it only shows the number up untill the comma, so 12,345 would come out as 12.
So supposedly there is a way to format the number indirectly with another parmater
SERIE_FORMAT=Theformat
This supposedly accepts a sprtinf() string that will format the affor mentioned numbers the way you want them to look.
An example that I was given was
SERIE_FORMAT=%0.1f
So I need to work out a format that will make integer numbers formated like currency (With or without the "$" sign) , but atleast have the commas.
You have access to the code that parses SERIE_DATA_1? If so, it would be easier to modify it to ignore commas then trying to re-invent the wheel to work around it.
Read how sprintf works. You'll find you will not be able to get it to work the way you wish. Printf allows for padding left/right, alignment left/right, width of how many characters the conversion will be done in, and precision. In here do you see it won't be possible to persuade sprintf to do the comma trick?
I'd recommend going back and figuring out how SERIE_DATA_1 is being parsed. Sounds like there's a short cut being taken and its botching something up. Or, after SERIE_DATA_1 is processed, then do your number conversion (but I'm figuring its already to late to do that easily).