Discussion The Ghastly And The Delightful

Discussion in 'PC Hardware and Software' started by fleshfragger, Aug 14, 2012.

Share This Page

  1. fleshfragger

    fleshfragger RAID Rookie

    Messages:
    343
    Likes Received:
    141
    Reputation:
    560
    We all have come across code snippets, practices which have simply boggled our minds. Some are downright horrendous, and some are truly stellar.

    This thread is the repository of the best , the worst, and the most interesting code snippets you have come across.


    Ill start:-

    Help me decipher the output of this function! :D

    public static string ConvertToReadableNumber(decimal num)
    {
    bool isNegative = num<0;
    num = Math.Floor(num);
    string numTxt = Math.Abs(num).ToString();
    string numReadable = "";
    int counter =0;
    for (int i = numTxt.Length-1; i >=0; i--)
    {
    if (counter > 0 && counter % 3 == 0)
    numReadable = "," + numReadable;
    numReadable = numTxt + numReadable;
    counter++;
    }
    if(isNegative)
    numReadable = "-"+numReadable;
    return numReadable;
    }
     
    JD666 likes this.
  2. MrsJD666

    MrsJD666 Ruling the Devil Staff Member

    Messages:
    319
    Likes Received:
    390
    Reputation:
    1,622
    I have always wondered how you guys understand that..

    :clap: for all you programmers.
     
  3. harryneopotter

    harryneopotter NEO is my middle name ! Staff Member

    Messages:
    1,161
    Likes Received:
    606
    Reputation:
    1,629
    Its a curse, mon ami, a curse. Sigh!!

    not sent from my pc....
     
  4. dbg

    dbg RAID Staff Staff Member

    Messages:
    3,111
    Likes Received:
    1,144
    Reputation:
    2,952
    public static string ConvertToReadableNumber(decimal num)
    {
    bool isNegative = num<0; -- if num is less then zero then isNegative becomes = 1
    num = Math.Floor(num); -- takes the absolute value of the input number, ignores the decimals & rounds-down the value.
    string numTxt = Math.Abs(num).ToString(); -- converts the number 123 into "123" string
    string numReadable = ""; -- initialize an empty string to store our output
    int counter =0; -- a counter variable.
    for (int i = numTxt.Length-1; i >=0; i--) -- the main loop to traverse the "123" string
    {
    if (counter > 0 && counter % 3 == 0) -- parses the string in parts of 3 .. eg. "1234567" will be parsed like "765" & "432" & "1" . This happens in reverse
    numReadable = "," + numReadable; -- add a "," to numReadable after every 3 digits are parsed
    numReadable = numTxt + numReadable; -- adds the current digit being parsed to the output string 'numReadable'
    counter++; -- counter increment
    }
    if(isNegative) --Checks if isNegative is = 1
    numReadable = "-"+numReadable; --if input was negative then add "-" to the start of the output string 'numReadable'
    return numReadable; -- return the final value of numReadable to parent function
    }


    So basically this program/function .. takes a decimal number as input like '1234556' and gives us a readable output like "1,234,556" by adding the comma the right places.


    MrsJD666 this is as simple as it gets .. :)
     
    Latest Given Reputation Points:
    fleshfragger: 14 Points (nice!) Aug 14, 2012
    harryneopotter and fleshfragger like this.
  5. Mrsabhi0008

    Mrsabhi0008 RAID Rookie

    Messages:
    17
    Likes Received:
    18
    Reputation:
    25
    Oops.:whatisthis::banghead:..
     
    MrsJD666 likes this.
  6. BANHAMMER

    BANHAMMER Teh Almighty BanHammer Staff Member

    Messages:
    5,046
    Likes Received:
    1,434
    Reputation:
    2,993
    oh man, i will post sources from linux kernel, that would be hilarious.
     
  7. BANHAMMER

    BANHAMMER Teh Almighty BanHammer Staff Member

    Messages:
    5,046
    Likes Received:
    1,434
    Reputation:
    2,993
  8. dbg

    dbg RAID Staff Staff Member

    Messages:
    3,111
    Likes Received:
    1,144
    Reputation:
    2,952
    Dont worry .. I tagged you by mistake first .. :p
     
  9. Mrsabhi0008

    Mrsabhi0008 RAID Rookie

    Messages:
    17
    Likes Received:
    18
    Reputation:
    25
    Lol :whew: , never mind
     
  10. MrsJD666

    MrsJD666 Ruling the Devil Staff Member

    Messages:
    319
    Likes Received:
    390
    Reputation:
    1,622
    I am guessing - with great powers, comes great responsibility!

    Its quarter to 6 in the morning. It was a long day. And a decently long night, we all know why.. I am far far far away from programming right now (not that I am ever close to it). I am a hardware person. So please dont mind if you find me 'dumber that I seem to be' when it comes to softwares.
     
  11. BANHAMMER

    BANHAMMER Teh Almighty BanHammer Staff Member

    Messages:
    5,046
    Likes Received:
    1,434
    Reputation:
    2,993
    for (int i = 0; i < 5; i++)
    p++;

    /* ROFL, what a looser. */
     
  12. harryneopotter

    harryneopotter NEO is my middle name ! Staff Member

    Messages:
    1,161
    Likes Received:
    606
    Reputation:
    1,629
    :wtf:
     
  13. BANHAMMER

    BANHAMMER Teh Almighty BanHammer Staff Member

    Messages:
    5,046
    Likes Received:
    1,434
    Reputation:
    2,993
    :whatisthis:
     

Share This Page