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. General Programming
  3. C#
  4. last 3 numbers

last 3 numbers

Scheduled Pinned Locked Moved C#
csstutorialquestion
6 Posts 5 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.
  • _ Offline
    _ Offline
    _Q12_
    wrote on last edited by
    #1

    If i have this number: 100000000000 my code will read it like this:

    if (rol > 99999999999) label1.Text = get3digit(rol) + " miliarde";
    ...
    //where get3digit(rol) method is this:
    int get3digit(Int64 myVal)
    {
    char mg1 = myVal.ToString()[0];
    char mg2 = myVal.ToString()[1];
    char mg3 = myVal.ToString()[2];
    char[] chars = { mg1, mg2, mg3 };
    string mg = new string(chars);
    int i = int.Parse(mg);
    return i;
    }
    //---RESULT is: "100 miliarde" (for this example) (but up to "999 miliarde");

    999999999999 (it means 999 miliarde) and is the Maximum value i will make the code. Now, I want to split this number by 3 LAST characters !!! So it will look like this in the end: "100 000 000 000" (yes, with spaces) Also for other LESS numbers than this one. 100 000 000 000 10 000 000 000 1 000 000 000 100 000 000 10 000 000 1 000 000 100 000 10 000 1 000 and this is the last value to insert the space. So 9 cases. I give multiple of 10 example here to be easy to understand, but it will be other numbers between 0 and 999 milliard. My idea is to use -char- as i did already, to separate the last numbers. But... maybe there is some funky way of doing it more easily? Or AUTOMATIC/ Default way? Thank you very much.

    L L realJSOPR B 4 Replies Last reply
    0
    • _ _Q12_

      If i have this number: 100000000000 my code will read it like this:

      if (rol > 99999999999) label1.Text = get3digit(rol) + " miliarde";
      ...
      //where get3digit(rol) method is this:
      int get3digit(Int64 myVal)
      {
      char mg1 = myVal.ToString()[0];
      char mg2 = myVal.ToString()[1];
      char mg3 = myVal.ToString()[2];
      char[] chars = { mg1, mg2, mg3 };
      string mg = new string(chars);
      int i = int.Parse(mg);
      return i;
      }
      //---RESULT is: "100 miliarde" (for this example) (but up to "999 miliarde");

      999999999999 (it means 999 miliarde) and is the Maximum value i will make the code. Now, I want to split this number by 3 LAST characters !!! So it will look like this in the end: "100 000 000 000" (yes, with spaces) Also for other LESS numbers than this one. 100 000 000 000 10 000 000 000 1 000 000 000 100 000 000 10 000 000 1 000 000 100 000 10 000 1 000 and this is the last value to insert the space. So 9 cases. I give multiple of 10 example here to be easy to understand, but it will be other numbers between 0 and 999 milliard. My idea is to use -char- as i did already, to separate the last numbers. But... maybe there is some funky way of doing it more easily? Or AUTOMATIC/ Default way? Thank you very much.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2
           Int64 i = 100000000;
           Console.WriteLine( i.ToString( "### ### ###" ).TrimStart() );
      

      "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

      _ 1 Reply Last reply
      0
      • _ _Q12_

        If i have this number: 100000000000 my code will read it like this:

        if (rol > 99999999999) label1.Text = get3digit(rol) + " miliarde";
        ...
        //where get3digit(rol) method is this:
        int get3digit(Int64 myVal)
        {
        char mg1 = myVal.ToString()[0];
        char mg2 = myVal.ToString()[1];
        char mg3 = myVal.ToString()[2];
        char[] chars = { mg1, mg2, mg3 };
        string mg = new string(chars);
        int i = int.Parse(mg);
        return i;
        }
        //---RESULT is: "100 miliarde" (for this example) (but up to "999 miliarde");

        999999999999 (it means 999 miliarde) and is the Maximum value i will make the code. Now, I want to split this number by 3 LAST characters !!! So it will look like this in the end: "100 000 000 000" (yes, with spaces) Also for other LESS numbers than this one. 100 000 000 000 10 000 000 000 1 000 000 000 100 000 000 10 000 000 1 000 000 100 000 10 000 1 000 and this is the last value to insert the space. So 9 cases. I give multiple of 10 example here to be easy to understand, but it will be other numbers between 0 and 999 milliard. My idea is to use -char- as i did already, to separate the last numbers. But... maybe there is some funky way of doing it more easily? Or AUTOMATIC/ Default way? Thank you very much.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, you can specify how a number gets formatted on output without performing any char or string manipulation yourself. The basic mechanism consists of calling the ToString() method while providing a format pattern as well as an object of type NumberFormatInfo that holds your specific wishes. In this case it would be:

        NumberFormatInfo nfi = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
        nfi.NumberGroupSeparator = " ";
        int yourNumber=123456789;
        string yourFormattedNumber = yourNumber.ToString("#,0", nfi);

        Obviously you need to create nfi only once, you can reuse it as often as you want. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        1 Reply Last reply
        0
        • L Lost User
               Int64 i = 100000000;
               Console.WriteLine( i.ToString( "### ### ###" ).TrimStart() );
          

          "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

          _ Offline
          _ Offline
          _Q12_
          wrote on last edited by
          #4

          EXCELENT !!! Thank you very much.

          1 Reply Last reply
          0
          • _ _Q12_

            If i have this number: 100000000000 my code will read it like this:

            if (rol > 99999999999) label1.Text = get3digit(rol) + " miliarde";
            ...
            //where get3digit(rol) method is this:
            int get3digit(Int64 myVal)
            {
            char mg1 = myVal.ToString()[0];
            char mg2 = myVal.ToString()[1];
            char mg3 = myVal.ToString()[2];
            char[] chars = { mg1, mg2, mg3 };
            string mg = new string(chars);
            int i = int.Parse(mg);
            return i;
            }
            //---RESULT is: "100 miliarde" (for this example) (but up to "999 miliarde");

            999999999999 (it means 999 miliarde) and is the Maximum value i will make the code. Now, I want to split this number by 3 LAST characters !!! So it will look like this in the end: "100 000 000 000" (yes, with spaces) Also for other LESS numbers than this one. 100 000 000 000 10 000 000 000 1 000 000 000 100 000 000 10 000 000 1 000 000 100 000 10 000 1 000 and this is the last value to insert the space. So 9 cases. I give multiple of 10 example here to be easy to understand, but it will be other numbers between 0 and 999 milliard. My idea is to use -char- as i did already, to separate the last numbers. But... maybe there is some funky way of doing it more easily? Or AUTOMATIC/ Default way? Thank you very much.

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            This will work regardless of the type of number, and regardless of its value.

            long number = 100000000000;
            string value = string.Format("{0:#,##0}", number).Replace(",", " ");

            ".45 ACP - because shooting twice is just silly" - JSOP, 2010
            -----
            You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
            -----
            When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

            1 Reply Last reply
            0
            • _ _Q12_

              If i have this number: 100000000000 my code will read it like this:

              if (rol > 99999999999) label1.Text = get3digit(rol) + " miliarde";
              ...
              //where get3digit(rol) method is this:
              int get3digit(Int64 myVal)
              {
              char mg1 = myVal.ToString()[0];
              char mg2 = myVal.ToString()[1];
              char mg3 = myVal.ToString()[2];
              char[] chars = { mg1, mg2, mg3 };
              string mg = new string(chars);
              int i = int.Parse(mg);
              return i;
              }
              //---RESULT is: "100 miliarde" (for this example) (but up to "999 miliarde");

              999999999999 (it means 999 miliarde) and is the Maximum value i will make the code. Now, I want to split this number by 3 LAST characters !!! So it will look like this in the end: "100 000 000 000" (yes, with spaces) Also for other LESS numbers than this one. 100 000 000 000 10 000 000 000 1 000 000 000 100 000 000 10 000 000 1 000 000 100 000 10 000 1 000 and this is the last value to insert the space. So 9 cases. I give multiple of 10 example here to be easy to understand, but it will be other numbers between 0 and 999 milliard. My idea is to use -char- as i did already, to separate the last numbers. But... maybe there is some funky way of doing it more easily? Or AUTOMATIC/ Default way? Thank you very much.

              B Offline
              B Offline
              BillWoodruff
              wrote on last edited by
              #6

              public string HowManyMillion(int millions, string suffix) => $"{millions / 1000000} {suffix}";

              // use example: string millions = HowManyMillion(100000000, "miliarde");

              «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

              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