Improved precision for smaller decimals
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user