| | 1 | | namespace Songhay; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Static helpers for Math. |
| | 5 | | /// </summary> |
| | 6 | | public static class MathUtility |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Gets the digit in number. |
| | 10 | | /// </summary> |
| | 11 | | /// <param name="x">The x.</param> |
| | 12 | | /// <param name="place">The place (from right to left).</param> |
| | 13 | | public static byte? GetDigitInNumber(int x, int place) |
| 9 | 14 | | { |
| 9 | 15 | | var d = (int) Math.Pow(10, place - 1); |
| 9 | 16 | | if (d < 0) return null; |
| | 17 | |
|
| 9 | 18 | | return Convert.ToByte((x / d) % 10); |
| 9 | 19 | | } |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Gets the mantissa. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="x">The x.</param> |
| | 25 | | /// <param name="round">The round.</param> |
| | 26 | | public static double GetMantissa(double x, int round) |
| 2 | 27 | | { |
| 2 | 28 | | double mantissaValue = (x < 0 ? -1 : 1) * |
| 2 | 29 | | (Math.Abs(x) - Math.Floor(Math.Abs(x))); |
| | 30 | |
|
| 2 | 31 | | mantissaValue = Math.Round(mantissaValue, round); |
| | 32 | |
|
| 2 | 33 | | return mantissaValue; |
| 2 | 34 | | } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Truncates the number. |
| | 38 | | /// </summary> |
| | 39 | | /// <param name="x">The x.</param> |
| | 40 | | /// <remarks> |
| | 41 | | /// Silverlight does not have <c>Math.Truncate()</c>. |
| | 42 | | /// </remarks> |
| 2 | 43 | | public static double TruncateNumber(double x) => (x < 0 ? -1 : 1) * Math.Floor(Math.Abs(x)); |
| | 44 | | } |