The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

Formatting Numbers - The Hard Way

Last post 03-14-2007 3:24 PM by CDarklock. 27 replies.
Page 1 of 1 (28 items)
Sort Posts: Previous Next
  • 03-01-2007 5:16 PM

    • utoxin
    • Not Ranked
    • 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); 
    }
    
    
  • 03-01-2007 5:52 PM In reply to

    Re: Formatting Numbers - The Hard Way

    I just love the new verbs "dollarfy" and "commify."
  • 03-01-2007 5:54 PM In reply to

    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 :-)

      

  • 03-01-2007 6:44 PM In reply to

    • Pap
    • Top 100 Contributor
    • 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.
  • 03-01-2007 7:24 PM In reply to

    • RayS
    • Top 25 Contributor
    • 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.
  • 03-01-2007 8:24 PM In reply to

    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".
  • 03-01-2007 9:00 PM In reply to

    • Samah
    • Not Ranked
    • 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
  • 03-02-2007 4:09 AM In reply to

    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"?

  • 03-02-2007 4:22 AM In reply to

    • Daid
    • Top 100 Contributor
    • 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 ;)
  • 03-02-2007 5:10 AM In reply to

    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!
     

     

  • 03-02-2007 9:40 AM In reply to

    • rbowes
    • Top 50 Contributor
    • 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*
  • 03-02-2007 9:45 AM In reply to

    • Daniel15
    • Top 150 Contributor
    • 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
  • 03-02-2007 11:40 AM In reply to

    • utoxin
    • Not Ranked
    • 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.
     

  • 03-02-2007 12:10 PM In reply to

    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. 
  • 03-02-2007 12:14 PM In reply to

    • utoxin
    • Not Ranked
    • 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. :)
     

  • 03-02-2007 1:16 PM In reply to

    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 :)

  • 03-02-2007 1:29 PM In reply to

    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. 

  • 03-02-2007 3:26 PM In reply to

    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.

     

  • 03-02-2007 4:04 PM In reply to

    • utoxin
    • Not Ranked
    • 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. 

  • 03-02-2007 5:11 PM In reply to

    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

  • 03-02-2007 7:36 PM In reply to

    • triso
    • Top 100 Contributor
    • 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]
  • 03-03-2007 9:33 AM In reply to

    • GeneWitch
    • Top 75 Contributor
    • 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
  • 03-03-2007 12:54 PM In reply to

    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
  • 03-05-2007 12:25 PM In reply to

    Re: Formatting Numbers - The Hard Way

    utoxin:

    That COBOL bit is cool. 

    Oh no!  You said "COBOL is cool" out loud!  Now they'll come to kill us!  We've got to hide.

  • 03-05-2007 12:31 PM In reply to

    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.

     

  • 03-13-2007 5:51 PM In reply to

    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! 

    Filed under: , ,