在 c# 中获取数字的数字
本文关键字:数字 获取 | 更新日期: 2023-09-27 18:32:07
我希望能够在 C# 中从数字中获取任何数字,所以我创建了一个函数来执行此操作。我只用数学来得到数字。这是我的代码
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;
}
但是,有一条错误消息 - "错误 1 无法将类型'double'隐式转换为'int'。存在显式转换(您是否缺少强制转换?我认为 Math.Pow 返回双精度,因此它尝试将数字类型转换为双精度。非常感谢帮助:)
转换为整数?
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;
}
}