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);
      }


  • I just love the new verbs "dollarfy" and "commify."



  • @Saladin said:

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

      



  • 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.



  • @DaveK said:

    @Saladin said:

    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!



  • @DaveK said:

    @Saladin said:

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


  • @Carnildo said:

    @DaveK said:

    @Saladin said:

    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


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



  • @Anonymouse said:

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



  • @Carnildo said:

    @DaveK said:

    @Saladin said:

    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!
     

     



  • All I can say is, OMG @ the recursion! closes browser and tries to forget ever seeing that code



  • 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



  • 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.
     



  • 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. 



  • @PSWorx said:

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



  • @utoxin said:

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



  • @RayS said:

    @DaveK said:

    @Saladin said:

    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. 



  • 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.

     



  • @newfweiler said:

    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. 



  •  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



  • @DaveK said:

    @Saladin said:

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

    [b]A[/b]ny noun can be verbed.



  • Can i verb your noun? Expletive! Adjective noun adverb verb!

    Expletive!

    Expletive!

    Expletive!



  • 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,'.',',')



  • @utoxin said:

    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.



  • @PSWorx said:

     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.

     



  • @newfweiler said:

    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! 



  • @Corona688 said:

    @newfweiler said:

    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! 

    Is that standard C?  I can't find it in K&R.

     



  • @Corona688 said:

    Stop doublespacing everything.  Let me write! 

    In most WYSIWYG HTML editors you find on forum and blog software, Enter is a paragraph break and Shift+Enter is a line break. So a bare enter looks like

    this, while shift+enter looks like
    this.

    Tribal knowledge. Ought to be documented somewhere, but apparently isn't.

     


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.