[TestFixture] public class IntToArrayHelperTests : IntToArrayHelper { [Test] public void TestGetNumberAt() { Assert.AreEqual( 0 , GetNumberAt( 0 , 1 )); Assert.AreEqual( 1 , GetNumberAt( 1 , 1 )); Assert.AreEqual( 1 , GetNumberAt( 21 , 1 )); Assert.AreEqual( 1 , GetNumberAt( 10001 , 1 )); Assert.AreEqual( 2 , GetNumberAt( 21 , 2 )); Assert.AreEqual( 1 , GetNumberAt( 87654321 , 1 )); Assert.AreEqual( 2 , GetNumberAt( 87654321 , 2 )); Assert.AreEqual( 7 , GetNumberAt( 87654321 , 7 )); Assert.AreEqual( 8 , GetNumberAt( 87654321 , 8 )); } [Test] public void TestGetDigitCount() { Assert.AreEqual( 1 , GetDigitCount( 0 )); Assert.AreEqual(1, GetDigitCount(1)); Assert.AreEqual(1, GetDigitCount(9)); Assert.AreEqual(2, GetDigitCount(10)); Assert.AreEqual(10, GetDigitCount(Int32.MaxValue)); } [Test] public void TestConvert() { int [] a = new int [ 1 ] { 0 }; Assert.AreEqual(a, Convert( 0 )); int [] b = new int [ 1 ] { 1 }; Assert.AreEqual(b, Convert( 1 )); int [] c = new int [ 10 ] { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 }; Assert.AreEqual(c, Convert( 1234567890 )); int [] d = new int [ 9 ] { 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 }; Assert.AreEqual(d, Convert( 987654321 )); int [] max = new int [ 10 ] { 2 , 1 , 4 , 7 , 4 , 8 , 3 , 6 , 4 , 7 }; Assert.AreEqual(max, Convert(Int32.MaxValue)); } [Test] [ExpectedException( typeof (ArgumentOutOfRangeException))] public void TestConvertWithNegative() { Convert( - 1 ); } }
3、实现代码: public class IntToArrayHelper { protected int GetNumberAt( int value, int digit) { int contrastNumber = ( int )Math.Pow( 10 , digit); int digitCutter = ( int )Math.Pow( 10 , digit - 1 ); return (value - value / contrastNumber * contrastNumber) / digitCutter; } protected int GetDigitCount( int value) { if (value == 0 ) return 1 ; return ( int )Math.Log10(value) + 1 ; } public int [] Convert( int value) { if (value < 0 ) throw new ArgumentOutOfRangeException( " value " , " Not less than zero. " ); int digitCount = GetDigitCount(value); int [] result = new int [digitCount]; for ( int i = 0 ; i < digitCount; ++ i) { int index = digitCount - i - 1 ; result[index] = GetNumberAt(value, i + 1 ); } return result; } }