1

I have a number like 601511616 If all number's length is multiple of 3, how can a split the number into an array without making a string

Also, how can I count numbers in the int without making a string?

Edit: Is there a way to simply split the number, knowing it's always in a multiple of 3... good output should look like this: {616,511,601}

Scotty
  • 259
  • 1
  • 12
Asterisk
  • 19
  • 5

4 Answers4

4

You can use i % 10 in order to get the last digit of integer.
Then, you can use division by 10 for removing the last digit.

1234567 % 10 = 7  
1234567 / 10 = 123456

Here is the code sample:

int value = 601511616;
List<int> digits = new List<int>();

while (value > 0) 
{
    digits.Add(value % 10);
    value /= 10;
}

// digits is [6,1,6,1,1,5,1,0,6] now

digits.Reverse(); // Values has been inserted from least significant to the most

// digits is [6,0,1,5,1,1,6,1,6] now

Console.WriteLine("Count of digits: {0}", digits.Count); // Outputs "9"

for (int i = 0; i < digits.Count; i++) // Outputs "601,511,616"
{
    Console.Write("{0}", digits[i]); 
    if (i > 0 && i % 3 == 0) Console.Write(","); // Insert comma after every 3 digits
}

IDEOne working demonstration of List and division approach.

Actually, if you don't need to split it up but only need to output in 3-digit groups, then there is a very convenient and proper way to do this with formatting.
It will work as well :)

int value = 601511616;
Console.WriteLine("{0:N0}", value); // 601,511,616
Console.WriteLine("{0:N2}", value); // 601,511,616.00

IDEOne working demonstration of formatting approach.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
  • hi, thanks I just need the count of numbers for use, not output. @Scotty already got this part of the question perfectly – Asterisk Aug 12 '15 at 04:13
  • maybe you would know something about how to split the number? – Asterisk Aug 12 '15 at 04:14
  • @Asterisk Oh. I have described how to split the number. My script has splitted the number into an array of digits. Moreover, I have updated the answer and have given a code which lets you easily output number in 3-digit groups. Please, check this out: http://ideone.com/gSLo0Y – Yeldar Kurmangaliyev Aug 12 '15 at 04:15
1

I can't understand your question regarding how to split a number into an array without making a string - sorry. But I can understand the question about getting the count of numbers in an int.

Here's your answer to that question.

Math.Floor(Math.Log10(601511616) + 1) = 9

Edit: Here's the answer to your first question..

            var n = 601511616;
            var nArray = new int[3];
            for (int i = 0, numMod = n; i < 3; numMod /= 1000, i++)
                nArray[i] = numMod%1000;

Please keep in mind there's no safety in this operation.

Edit#3

Still not perfect, but a better example.

    var n = 601511616;
    var nLength = (int)Math.Floor(Math.Log10(n) + 1)/ 3;
    var nArray = new int[nLength];
    for (int i = 0, numMod = n; i < nLength; numMod /= 1000, i++)
        nArray[i] = numMod%1000;

Edit#3:

IDEOne example http://ideone.com/SSz3Ni

the output is exactly as the edit approved by the poster suggested.

{ 616, 511, 601 }
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Scotty
  • 259
  • 1
  • 12
  • this is good! thank you! I don'tknow how to explain better splitting a number – Asterisk Aug 12 '15 at 03:58
  • @asterisk Do you mean splitting it into something like this... [6,0,1,5,1,1,6,1,6] ? – Rob Aug 12 '15 at 04:00
  • Thank ?ou! That's exactly what I needed but you made a mistake in writing n%1000 instead of numMod%1000 and assigning numMode = n is perfect so it doesn't decrease a public int thanks – Asterisk Aug 12 '15 at 04:46
  • You're welcome, and I fixed my error. Thanks for pointing that out. I left another example, including a way to check to see if your number's length is a multiple of 3. – Scotty Aug 12 '15 at 04:52
  • how could I safley handle a case where my number isn't a multiple of 3 without throwing that exception? – Asterisk Aug 12 '15 at 04:54
  • @Asterisk Man, I do not understand you. I have provided a **correct**, **one-line** way to output your number in groups of 3. IDEOne link: http://ideone.com/o0aTS3. It even works with numbers of length not multiple of 3. Why have you chosen a 7-line code which does the same but manually? – Yeldar Kurmangaliyev Aug 14 '15 at 04:13
  • @Yeldar It seemed to me he was looking for a way to use the values without using strings at all. Plus the number grouping in the array should be the digits long according to his request. Perhaps his request was too proprietary? Ideone is neat btw, here's the code I provided running on it. http://ideone.com/SSz3Ni – Scotty Aug 14 '15 at 18:32
  • Also, can the downvotes be explained? It seems the answer fit his question. – Scotty Aug 14 '15 at 18:34
1

Using Log10 to calculate the number of digits is easy, but it involves floating-point operations which is very slow and sometimes incorrect due to rounding errors. You can use this way without calculating the value size first. It doesn't care if the number of digits is a multiple of 3 or not.

int value = 601511616;

List<int> list = new List<int>();

while (value > 0) // main part to split the number
{
    int t = value % 1000;
    value /= 1000;
    list.Add(t);
}

// Convert back to an array only if it's necessary, otherwise use List<T> directly
int[] splitted = list.ToArray();

This will store the splitted numbers in reverse order, i.e. 601511616 will become {616, 511, 601}. If you want the numbers in original order, simply iterate the array backwards. Alternatively use Array.Reverse or a Stack

phuclv
  • 37,963
  • 15
  • 156
  • 475
0

Since you already know they are in multiples of 3, you can just use the extracting each digit method but use 1000 instead of 10. Here is the example

a = 601511616
b = []

while(a):
    b.append(a%1000)
    a = a//1000

print(b)
#[616, 511, 601]