Skip to content

Instantly share code, notes, and snippets.

@BryanWilhite
Last active August 29, 2015 14:09
Show Gist options
  • Save BryanWilhite/405fc09edb27c02d2d02 to your computer and use it in GitHub Desktop.
Save BryanWilhite/405fc09edb27c02d2d02 to your computer and use it in GitHub Desktop.
C#: SetCollectionWithDigits()
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