Improved precision for smaller decimals

This commit is contained in:
clodan
2025-01-29 13:30:52 +00:00
parent 7c060fdf86
commit dbf3071664
2 changed files with 4 additions and 3 deletions
+3 -2
View File
@@ -414,10 +414,11 @@ public class RandomUtil(ISptLogger<RandomUtil> _logger, ICloner _cloner)
/// <returns>The number of decimal places, or 0 if none exist.</returns>
public int GetNumberPrecision(double num)
{
var preciseNum = (decimal)num;
var factor = 0;
while (num % 1 > double.Epsilon)
while ((double)(preciseNum % 1) > double.Epsilon)
{
num *= 10D;
preciseNum *= 10M;
factor++;
}
return factor;
+1 -1
View File
@@ -188,7 +188,7 @@ public sealed class RandomUtilTests
[DataRow(0.0001, 4)]
[DataRow(0, 0)]
[DataRow(10000000, 0)]
[DataRow(0.000_000_000_000_1D, 13)]
[DataRow(0.000_000_000_000_000_000_000_000_1D, 25)]
public void GetNumberPrecision_WithDoubles_ReturnsDecimalPoints(double value, int decimalPoints)
{
Assert.AreEqual(decimalPoints, _randomUtil.GetNumberPrecision(value));