| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | |
| | | 4 | | namespace DirectSight.Common; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Math extensions. |
| | | 8 | | /// </summary> |
| | | 9 | | internal static class MathExtensions |
| | | 10 | | { |
| | 1 | 11 | | private static int maximumDecimalPlaces = 1; |
| | | 12 | | |
| | 1 | 13 | | private static double factor = 1000; |
| | | 14 | | |
| | 1 | 15 | | private static int divisor = 10; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Gets or sets the maximum decimal places for coverage quotas / percentages. |
| | | 19 | | /// </summary> |
| | | 20 | | public static int MaximumDecimalPlaces |
| | | 21 | | { |
| | | 22 | | get |
| | 2 | 23 | | { |
| | 2 | 24 | | return maximumDecimalPlaces; |
| | 2 | 25 | | } |
| | | 26 | | |
| | | 27 | | set |
| | 20 | 28 | | { |
| | 20 | 29 | | maximumDecimalPlaces = Math.Min(8, Math.Max(0, value)); |
| | 20 | 30 | | factor = 100 * Math.Pow(10, maximumDecimalPlaces); |
| | 20 | 31 | | divisor = (int)Math.Pow(10, maximumDecimalPlaces); |
| | 20 | 32 | | } |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Creates a <see cref="HashSet<T>"/> from an <see cref="IEnumerable<T>"/>. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="number1">The first number.</param> |
| | | 39 | | /// <param name="number2">The second number.</param> |
| | | 40 | | /// <returns>The percentage.</returns> |
| | | 41 | | internal static decimal CalculatePercentage(int number1, int number2) |
| | 6288 | 42 | | { |
| | 6288 | 43 | | if (number2 == 0) |
| | 0 | 44 | | { |
| | 0 | 45 | | throw new ArgumentException("Number must not be 0", nameof(number2)); |
| | | 46 | | } |
| | | 47 | | |
| | 6288 | 48 | | return (decimal)Math.Truncate(factor * (double)number1 / (double)number2) / divisor; |
| | 6288 | 49 | | } |
| | | 50 | | } |