Last active
August 29, 2015 14:09
-
-
Save BryanWilhite/405fc09edb27c02d2d02 to your computer and use it in GitHub Desktop.
C#: SetCollectionWithDigits()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class Sample | |
{ | |
/// <summary> | |
/// Sets the collection with digits. | |
/// </summary> | |
/// <param name="number">The number.</param> | |
/// <param name="collection">The collection.</param> | |
public static void SetCollectionWithDigits(double number, ObservableCollection<byte?> collection) | |
{ | |
if (collection == null) return; | |
var mantissaDigits = 2; //TODO: allow digits to be variable. | |
var x = Convert.ToInt32(100 * MathUtility.GetMantissa(number, mantissaDigits)); | |
if (collection.Count < mantissaDigits) return; | |
collection[0] = (x >= 1e0) ? MathUtility.GetDigitInNumber(x, 1) : 0; | |
collection[1] = (x >= 1e1) ? MathUtility.GetDigitInNumber(x, 2) : 0; | |
x = Convert.ToInt32(MathUtility.TruncateNumber(number)); | |
for (int i = 2; i < collection.Count; i++) | |
{ | |
var place = i - 1; | |
var y = i - 2; | |
collection[i] = (x < Math.Pow(10, y)) ? 0 : MathUtility.GetDigitInNumber(x, place); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment