public static NumberDigits Convert(double number) { NumberDigits digits = new NumberDigits(number); double decimalPart = Math.Round(number - (int)number, 15); double multiplier = 10; double divValue = 0.1; while (decimalPart > 0) { double v1 = decimalPart * multiplier; int digit = (int)v1; decimalPart = Math.Round(v1 - digit, 15); digits.Digits.Add(divValue, digit); divValue /= 10; } // integer part int integerPart = (int)number; int divisor = 10; double mulValue = 1; while (integerPart > 0) { int digit = integerPart % divisor; integerPart -= digit; integerPart = integerPart / divisor; digits.Digits.Add(mulValue, digit); mulValue *= 10; } return(digits); }
public static void Test() { Console.WriteLine("Test Digits Conversion: "); var digits1 = NumberDigits.Convert(122); Console.WriteLine(digits1); var digits2 = NumberDigits.Convert(122.134); Console.WriteLine(digits2); // get specific digits Console.WriteLine(); }
static void Main(string[] args) { //BalancedParentheses.Test(); //Aggregates.Test(); //SelectionSort.Test(); //BubbleSort.Test(); //InsertionSort.Test(); //MergeSort<int>.Test(); //LinearSearch.Test(); //BinarySearch.Test(); //Recursion.Test(); //DigitsAnalysis.Test(); NumberDigits.Test(); }