Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. ConvertToReadableNumber

ConvertToReadableNumber

Scheduled Pinned Locked Moved The Weird and The Wonderful
questioncareer
13 Posts 12 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Arpikusz

    Can anyone tell me what this code does? :-D I found it in a project I inherited:

    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[i] + numReadable;
    counter++;
    }
    if(isNegative)
    numReadable = "-"+numReadable;
    return numReadable;
    }

    I'm guessing that a ToString() would have done the job. :)

    B Offline
    B Offline
    Bernhard Hiller
    wrote on last edited by
    #4

    The real wtf is the enormous variation in the paramters of the conversion function. When you know how to do it correctly with VB6, it won't work with C++, C#, Oracle SQL, MySQL, Microsoft SQL Server, ... I guess the guy who wrote the code was a victim of that diversity, and wrote it after switching to C# from something else.

    1 Reply Last reply
    0
    • A Arpikusz

      Can anyone tell me what this code does? :-D I found it in a project I inherited:

      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[i] + numReadable;
      counter++;
      }
      if(isNegative)
      numReadable = "-"+numReadable;
      return numReadable;
      }

      I'm guessing that a ToString() would have done the job. :)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #5

      ToString would have done a better job as it's not depending on the culture. Lot's of companies that have utility-functions like this in their "toobox", often rebuilding what's already there. To me, that proves that most of these companies are too rich. If they had to be careful on what they'd spend, shit like this would be gone.

      Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

      1 Reply Last reply
      0
      • A Arpikusz

        Can anyone tell me what this code does? :-D I found it in a project I inherited:

        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[i] + numReadable;
        counter++;
        }
        if(isNegative)
        numReadable = "-"+numReadable;
        return numReadable;
        }

        I'm guessing that a ToString() would have done the job. :)

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #6

        Arpikusz wrote:

        numReadable = "," + numReadable;

        What happens if the culture is one that uses . instead of , for the separator? Not only is this bad code, it's lazy bad code because it's not taking localisation into account. Also, as I look over the code, what happens if num is 3000.27? I can't see anywhere in this code that actually adds the portion after the decimal on to numReadable.

        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        D 1 Reply Last reply
        0
        • P Pete OHanlon

          Arpikusz wrote:

          numReadable = "," + numReadable;

          What happens if the culture is one that uses . instead of , for the separator? Not only is this bad code, it's lazy bad code because it's not taking localisation into account. Also, as I look over the code, what happens if num is 3000.27? I can't see anywhere in this code that actually adds the portion after the decimal on to numReadable.

          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #7

          And that's why it is a readable number. Use whole numbers to avoid confusions like '.' or ','. :)

          "Fear no factor", Prime Numbers.

          A 1 Reply Last reply
          0
          • J J4amieC

            Its an abomination, for which the author should be sumarily executed. It takes a decimal, floors it, and outputs it with commas as the thousand saparator: http://rextester.com/CAYFY90912[^] It boils down to this one-liner:

            var result = Math.Floor(num).ToString("#,#");

            B Offline
            B Offline
            Brisingr Aerowing
            wrote on last edited by
            #8

            Cool site! And I agree!

            Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions. Dave Barry Read more at [BrainyQuote](http://www.brainyquote.com/quotes/topics topic_technology.html#yAfSEbrfumitrteO.99)[^]

            1 Reply Last reply
            0
            • A Arpikusz

              Can anyone tell me what this code does? :-D I found it in a project I inherited:

              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[i] + numReadable;
              counter++;
              }
              if(isNegative)
              numReadable = "-"+numReadable;
              return numReadable;
              }

              I'm guessing that a ToString() would have done the job. :)

              J Offline
              J Offline
              JeremyBob
              wrote on last edited by
              #9

              Um, isn't Math.Floor(-12.45) == -13?

              1 Reply Last reply
              0
              • A Arpikusz

                Can anyone tell me what this code does? :-D I found it in a project I inherited:

                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[i] + numReadable;
                counter++;
                }
                if(isNegative)
                numReadable = "-"+numReadable;
                return numReadable;
                }

                I'm guessing that a ToString() would have done the job. :)

                W Offline
                W Offline
                Wenff
                wrote on last edited by
                #10

                Speechless :omg: .

                1 Reply Last reply
                0
                • J J4amieC

                  Its an abomination, for which the author should be sumarily executed. It takes a decimal, floors it, and outputs it with commas as the thousand saparator: http://rextester.com/CAYFY90912[^] It boils down to this one-liner:

                  var result = Math.Floor(num).ToString("#,#");

                  B Offline
                  B Offline
                  bojanh
                  wrote on last edited by
                  #11

                  If you really want to be precise and duplicate his function, it's actually this: var result = Math.Floor(num).ToString("#,#", System.Globalization.CultureInfo.InvariantCulture); Perhaps the author hard coded the thousand separator for some reason...

                  1 Reply Last reply
                  0
                  • A Arpikusz

                    Can anyone tell me what this code does? :-D I found it in a project I inherited:

                    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[i] + numReadable;
                    counter++;
                    }
                    if(isNegative)
                    numReadable = "-"+numReadable;
                    return numReadable;
                    }

                    I'm guessing that a ToString() would have done the job. :)

                    S Offline
                    S Offline
                    Sasha Laurel
                    wrote on last edited by
                    #12

                    if its C#, how about the standard .net formatting codes:

                    Math.Floor(num).ToString("N0")

                    1 Reply Last reply
                    0
                    • D dan sh

                      And that's why it is a readable number. Use whole numbers to avoid confusions like '.' or ','. :)

                      "Fear no factor", Prime Numbers.

                      A Offline
                      A Offline
                      all_in_flames
                      wrote on last edited by
                      #13

                      Agreed, clearly the assumption here is that a decimal number is not readable :)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups