在Base 10中返回整数

use*_*879 0 c# base

我一直试图理解如何输入一个整数并使用函数返回C#中基数为10的数字.我已经研究过,除了数学公式之外,找不到很多代码示例.

谢谢!

Jon*_*eet 5

听起来你只是想要:

int value = 2590123;
string text = value.ToString();
Run Code Online (Sandbox Code Playgroud)

这将自动使用基数10 ...至少在我所知道的所有文化中.如果你真的想确定,请使用不变文化:

string text = value.ToString(CultureInfo.InvariantCulture);
Run Code Online (Sandbox Code Playgroud)

请注意,当您使用某种形式的单独"数字"(例如字符串表示)来讨论某些表示时,基本概念才有意义.一个纯数字没有基数 - 如果你有16个苹果,那就像你有0x10苹果一样.

编辑:或者如果你想编写一个方法来返回数字序列作为整数,最不重要的是:

// Note that this won't give pleasant results for negative input
static IEnumerable<int> GetDigits(int input)
{
    // Special case...
    if (input == 0)
    {
        yield return 0;
        yield break;
    }
    while (input != 0)
    {
        yield return input % 10;
        input = input / 10;
    }
}
Run Code Online (Sandbox Code Playgroud)