1

This is my first post on Stack Overflow ever. If I do it wrong, please correct me:

Using C#, I'd like to convert a double to an int array of variable length equal to the total number of digits in the double.

for example, if I had the number 35.777778, I would want to convert it an int array where

array[0] = 3
array[1] = 5
array[2] = 7
etc.

I plan to iterate through the array and compare array[i] to array[i + 1].

The double will be the quotient of two variable numbers. So it may have very few digtis or a lot of digits.

So far I have tried to convert it to a string and then to an array of ints. But it crashes at the decimal point.

Please help me...

5
  • This might be of interest stackoverflow.com/questions/1040707/… Commented Apr 21, 2015 at 23:05
  • Why do you need an array of integers, when the maximum value of a digit is 10? Commented Apr 21, 2015 at 23:07
  • such array is called 'a string' Commented Apr 21, 2015 at 23:13
  • 2
    @Alex you should know better. It is 9. :D Commented Apr 21, 2015 at 23:17
  • I need to pull out the single digits, 0 through 9. Commented Apr 22, 2015 at 22:11

4 Answers 4

1

This should do it:

int[] array = 35.777778.ToString().Replace(".", String.Empty)
                                  .Select(c => (int)Char.GetNumericValue(c))
                                  .ToArray();
Sign up to request clarification or add additional context in comments.

3 Comments

This works! I did manage to solve it on my own, but using A LOT more code than this. Thank you Chomba!
Problem: This solution works just dandy in a console application. But when I try to add it to a portable library it says there is no such animal as "String.select ". Is there a reference I need to add or something along those lines?
Well that's because String doesn't implement IEnumerable<char> in PCLs. So you have to convert the string to a char array with ToCharArray(), like this: .Replace(...).ToCharArray().Select(...
0

You could do it the way you were doing (convert to string and then parse each char to an int) and just strip out the decimal point. Like so:

var nums = decimalNum.ToString().Replace('.', '').ToCharArray();
int[] numbers = new int[nums.Count];
var x = 0;
foreach (var item in nums)
{
    numbers[x] = Integer.ParseInt(item);
    x++;
}

This obviously isn't the prettiest solution, but it would work.

Comments

0

Floating point to decimal digit conversion is a hard problem. In .NET, the tool have available is really the double.ToString method which is bloated and especially bad when you want the digits. As you see the other answers all have to parse back to integer.

If performance is important there are good double to string conversion algorithms out there that can be modified to fit your needs. This one is good:

C# port of google's Grisu conversion

Comments

0
int[] array = 35.777778.ToString().Where(c => char.IsDigit(c)).Select(c1 => Convert.ToInt32(c1.ToString())).ToArray();

This code will work even if the double value contains any charater like E-10

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.