I want to be able to take any digit from a number in C# , so I created a function to do so. I used only maths to get to the digit. Here is my code
static int GetDigit(int number, int k)
{
// k is the positiong of the digit I want to get from the number
// I want to divide integer number to 10....0 (number of 0s is k) and then % 10
// to get the last digit of the new number
return (number / (int)Math.Pow(10, k-1)) % 10;
}
However, there is an error message - "Error 1 Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)". I think that Math.Pow returns double so it tries to convert the type of number to double. Help is greatly appreciated :)
Convert to integer?
static int GetDigit(int number, int k)
{
// k is the positiong of the digit I want to get from the number
// I want to divide integer number to 10....0 (number of 0s is k) and then % 10
// to get the last digit of the new number
return (int)(number / Math.Pow(10, k)) % 10;
}
}