|
Formatting Numbers - The Hard Way
Last post 03-14-2007 3:24 PM by CDarklock. 27 replies.
-
03-01-2007 5:16 PM
|
|
-
utoxin


- Joined on 11-24-2004
- Springville, UT
- Posts 16
|
Formatting Numbers - The Hard Way
Found this while going through some code I was asked to edit. I'm working with a Major Search Engine, and this was in one of the include files for a tool that is used to analyze advertiser keyword performance.
Sadly, it can all be replaced with one built in PHP function call: number_format($number, $decimals)
/*****************************************************************************************
*
* Dollarfy will take any number and turn it into the USD representation of the number
* NOTE: Does not include the $, but will round the number and add commas where necessary
*
* Pass the Number, defaults to two decimal places
*****************************************************************************************/
function dollarfy ($num='',$dec='2') {
$dec = $dec ? $dec : 0;
$format="%.$dec" . "f";
$number=sprintf($format,$num);
$str=strtok($number,".");
$dc=strtok(".");
$str=$this->commify($str);
$return="$str";
if ($dec!=0) {
$return = "$return" . ".$dc";
}
return($return);
}
/*****************************************************************************************
*
* Commify will take any number and add commas where necessary
*
* Pass the Number, defaults to two decimal places
*****************************************************************************************/
function commify ($str) {
$n = strlen($str);
if ($n <= 3) {
$return=$str;
} else {
$pre=substr($str,0,$n-3);
$post=substr($str,$n-3,3);
$pre=$this->commify($pre);
$return="$pre,$post";
}
return($return);
}
|
|
-
-
Saladin


- Joined on 10-02-2006
- Posts 127
|
Re: Formatting Numbers - The Hard Way
I just love the new verbs "dollarfy" and "commify."
|
|
-
-
DaveK


- Joined on 02-22-2006
- Posts 476
|
Re: Formatting Numbers - The Hard Way
Saladin:I just love the new verbs "dollarfy" and "commify."
One of the much-commented-on quirks of the English language is the way you can verbafy pretty much any noun :-)
|
|
-
-
Pap


- Joined on 09-12-2006
- Earf
- Posts 281
|
Re: Formatting Numbers - The Hard Way
It's a good thing he commented his code, otherwise I would have thought commify() is how you take a string and make it join a political party.
|
|
-
-
RayS


- Joined on 01-22-2005
- Posts 680
|
Re: Formatting Numbers - The Hard Way
DaveK: Saladin:I just love the new verbs "dollarfy" and "commify."
One of the much-commented-on quirks of the English language is the way you can verbafy pretty much any noun :-)
Not to mention that you can nounify verbs! I love those, and hope that the system has a few more, such as stringify, intify, floatify, etc. Of course, if you live the OO lifestyle, you can go around objectifying things!
EULA: By reading this post and associated disclaimer, you are consenting to agree with the opinions disclosed within. If you disagree with this license agreement, you may not return it for a refund.
|
|
-
-
Carnildo


- Joined on 03-30-2005
- Posts 651
|
Re: Formatting Numbers - The Hard Way
DaveK: Saladin:I just love the new verbs "dollarfy" and "commify."
One of the much-commented-on quirks of the English language is the way you can verbafy pretty much any noun :-)
The proper spelling is "verbify", or simply "verb".
|
|
-
-
Samah


- Joined on 04-06-2006
- Posts 39
|
Re: Formatting Numbers - The Hard Way
Carnildo: DaveK: Saladin:I just love the new verbs "dollarfy" and "commify."
One of the much-commented-on quirks of the English language is the way you can verbafy pretty much any noun :-)
The proper spelling is "verbify", or simply "verb".
Hah I didn't want to be the one to say that although I was thinking it. :D
|
|
-
-
Anonymouse


- Joined on 01-16-2006
- Posts 44
|
Re: Formatting Numbers - The Hard Way
Okay, my PHP skills may be a bit rusty, but I don't see a loop there... Wouldn't that mean that "1024681.234" would be rendered as "1024,681.23"?
|
|
-
-
Daid


- Joined on 01-30-2007
- Posts 283
|
Re: Formatting Numbers - The Hard Way
Anonymouse:Okay, my PHP skills may be a bit rusty, but I don't see a loop there... Wouldn't that mean that "1024681.234" would be rendered as "1024,681.23"?
It's resursive calling itself ;)
|
|
-
-
DaveK


- Joined on 02-22-2006
- Posts 476
|
Re: Formatting Numbers - The Hard Way
Carnildo: DaveK: Saladin:I just love the new verbs "dollarfy" and "commify."
One of the much-commented-on quirks of the English language is the way you can verbafy pretty much any noun :-)
The proper spelling is "verbify", or simply "verb".
I know, but I wanted it to sound more like 'dollarfy'. I claim poetic license!
|
|
-
-
rbowes


- Joined on 02-09-2007
- Winnipeg, MB
- Posts 412
|
Re: Formatting Numbers - The Hard Way
All I can say is, OMG @ the recursion! *closes browser and tries to forget ever seeing that code*
|
|
-
-
Daniel15


- Joined on 01-27-2007
- Melbourne, Australia
- Posts 213
|
Re: Formatting Numbers - The Hard Way
That commify function is quite funny... It says "defaults to two decimal places", but there's no way to change this "default", and it doesn't even handle decimals properly.
If you pass it 123456789.987654321, the output is 1,234,567,89.,988
|
|
-
-
utoxin


- Joined on 11-24-2004
- Springville, UT
- Posts 16
|
Re: Formatting Numbers - The Hard Way
I'm glad you found it as amusing and awful as I did. I considered submitting it for a Code SOD, but decided I'd rather make sure it got seen. :) I must say, seeing this code in production sure eased my worry about whether I'd be up for the pressure for working for said Major Search Engine. If that kind of garbage can get by, my code will certainly be alright. After all, while I may use a 'junk' language, according to many people, I pride myself in writing the best code I possibly can.
|
|
-
-
PSWorx


- Joined on 04-28-2006
- Posts 713
|
Re: Formatting Numbers - The Hard Way
Despite the obvious WTFness, this seems to me like a typical case of the pathological string programmer, who tries to solve all problems by converting the input to a string, bludgeoning it with string manipulation functions until it kinda looks like the desired result and then converting it back. Btw, am I the only one who finds
function dollarfy ($num='',$dec='2') { $dec = $dec ? $dec : 0;a bit um ... wrongfully redundant? Please tell me when exactly the 'else' expression of the ternary operator is evaluated there.
|
|
-
-
utoxin


- Joined on 11-24-2004
- Springville, UT
- Posts 16
|
Re: Formatting Numbers - The Hard Way
PSWorx:Despite the obvious WTFness, this seems to me like a typical case of the pathological string programmer, who tries to solve all problems by converting the input to a string, bludgeoning it with string manipulation functions until it kinda looks like the desired result and then converting it back. Btw, am I the only one who finds
function dollarfy ($num='',$dec='2') { $dec = $dec ? $dec : 0;a bit strange? Please tell me when exactly the 'else' expression of the ternary operator is evaluated there.
It would get evaluated if someone passed in a non-true value as the second arguement. Which could happen if... uh... someone used a function call for the second parameter, and it failed, or something? *makes stuff up to try and justify this* And yeah, while this may be a 'string programmer' symptom... that doesn't excuse it. I started out in Perl, which is a prime breeding ground for string programmers. And I would NEVER consider formatting a string that way. :)
|
|
-
-
PSWorx


- Joined on 04-28-2006
- Posts 713
|
Re: Formatting Numbers - The Hard Way
utoxin:And yeah, while this may be a 'string programmer' symptom... that doesn't excuse it.
Pardon my bad English, I meant to say "in addition to" instead of "despite". I didn't want to excuse it in any way :)
|
|
-
-
lizardfoot


- Joined on 03-07-2006
- Posts 41
|
Re: Formatting Numbers - The Hard Way
RayS: DaveK: Saladin:I just love the new verbs "dollarfy" and "commify."
One of the much-commented-on quirks of the English language is the way you can verbafy pretty much any noun :-)
Not to mention that you can nounify verbs! I love those, and hope that the system has a few more, such as stringify, intify, floatify, etc. Of course, if you live the OO lifestyle, you can go around objectifying things!
Makes ME want to slappify someone.
|
|
-
-
newfweiler


- Joined on 10-18-2006
- Newf Hampshire
- Posts 373
|
Re: Formatting Numbers - The Hard Way
Well, I wrote a function named "strCommaIze" once, in C, because there wasn't anything built-in. I still use it sometimes.
<*sigh*> I remember the good old days of COBOL. It was so easy. Just like this:
77 WITH-COMMAS PICTURE Z,ZZZ,ZZZ,ZZ9.
MOVE RESULT-VALUE TO WITH-COMMAS.
|
|
-
-
utoxin


- Joined on 11-24-2004
- Springville, UT
- Posts 16
|
Re: Formatting Numbers - The Hard Way
newfweiler:Well, I wrote a function named "strCommaIze" once, in C, because there wasn't anything built-in. I still use it sometimes.
<*sigh*> I remember the good old days of COBOL. It was so easy. Just like this:
77 WITH-COMMAS PICTURE Z,ZZZ,ZZZ,ZZ9.
MOVE RESULT-VALUE TO WITH-COMMAS.
If there isn't something available, that's great. Writing your own is perfectly alright. (Assuming you make it logical). But when a language has a well documented standard function that does /exactly/ what you need, only much better than your weird custom function, you should probably use it. :) That COBOL bit is cool.
|
|
-
-
PSWorx


- Joined on 04-28-2006
- Posts 713
|
Re: Formatting Numbers - The Hard Way
Quote: Well, I wrote a function named "strCommaIze" once, in C, because there wasn't anything built-in. I still use it sometimes.
<*sigh*> I remember the good old days of COBOL. It was so easy. Just like this:
77 WITH-COMMAS PICTURE Z,ZZZ,ZZZ,ZZ9.
MOVE RESULT-VALUE TO WITH-COMMAS. ...but how am I supposed to order that ISS vacation trip for 100 billions from your catalogue? :p
|
|
-
-
triso


- Joined on 04-08-2005
- Ontario, Canada
- Posts 256
|
Re: Formatting Numbers - The Hard Way
DaveK: Saladin:I just love the new verbs "dollarfy" and "commify."
One of the much-commented-on quirks of the English language is the way you can verbafy pretty much any noun :-) Any noun can be verbed.
[My signature is four letters short from becoming rude]
|
|
-
-
GeneWitch


- Joined on 12-23-2006
- Orange County, CA
- Posts 310
|
Re: Formatting Numbers - The Hard Way
Can i verb your noun? Expletive! Adjective noun adverb verb!
Expletive!
Expletive!
Expletive!
sudo make me a sandwich
|
|
-
-
stratos


- Joined on 09-06-2006
- Posts 394
|
Re: Formatting Numbers - The Hard Way
It's even beter, the built in function can handle forein money notations, the 'dollarfy' can't. function moneyfy($amount) { return nummber_format($amount,2,'.',',')
}
"Show me a sane man and I will cure him for you." - C. G. Jung
|
|
-
-
-
newfweiler


- Joined on 10-18-2006
- Newf Hampshire
- Posts 373
|
Re: Formatting Numbers - The Hard Way
PSWorx:
Quote:
Well, I wrote a function named "strCommaIze" once, in C, because there wasn't anything built-in. I still use it sometimes.
<*sigh*> I remember the good old days of COBOL. It was so easy. Just like this:
77 WITH-COMMAS PICTURE Z,ZZZ,ZZZ,ZZ9.
MOVE RESULT-VALUE TO WITH-COMMAS.
...but how am I supposed to order that ISS vacation trip for 100 billions from your catalogue? :p
That's a limitation of COBOL, and machine language, and punch cards, and pocket calculators, and most non-interpretive languages. You have to declare everything of a certain size.
COBOL originally had a hard limit of 18 digits. This was partly to make implementation equally difficult for all computer companies because it was wider than the hardware limit on all the computers.
When you write in C all you can do is declare short, int, long, and sometimes long long. (Isn't he a pianist?) But C does not define the range of a short, int, long, or long long.
|
|
-
-
Corona688


- Joined on 12-14-2006
- Posts 16
|
Re: Formatting Numbers - The Hard Way
newfweiler:When you write in C all you can do is declare short, int, long, and sometimes long long. (Isn't he a pianist?) But C does not define the range of a short, int, long, or long long.
#include <stdint.h> int32_t val=INT32_MAX; ...damnit Alex. Stop doublespacing everything. Let me write! Let me write!
|
|
|
|