Split number into hundreds tens and units using C#

前端 未结 5 1026
星月不相逢
星月不相逢 2021-01-03 13:00

What is the best way to split some given larger number into hundreds, tens and units in C#?

For example: If I enter number 43928 how can I get 40000 + 3000 + 900 + 2

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-03 13:02

    The way with the parsing:

    var myBigNumber = 43928.ToString();
    var asCharachters = myBigNumber.ToArray();
    for (var i = 0; i < asCharachters.Length; i++)
    {
        var numberOfZeros = asCharachters.Length - i;
        var itemAsString = (asCharachters[i]).ToString().PadRight(numberOfZeros, '0');
        Console.WriteLine( Int32.Parse(itemAsString));
    }
    

    Outputs this:

    40000
    3000
    900
    20
    8
    

提交回复
热议问题