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}

0

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

hi, thanks I just need the count of numbers for use, not output. @Scotty already got this part of the question perfectly
maybe you would know something about how to split the number?
@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: ideone.com/gSLo0Y
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 } 

8 Comments

this is good! thank you! I don'tknow how to explain better splitting a number
@asterisk Do you mean splitting it into something like this... [6,0,1,5,1,1,6,1,6] ?
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
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.
how could I safley handle a case where my number isn't a multiple of 3 without throwing that exception?
|
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

Comments

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] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.