-
Notifications
You must be signed in to change notification settings - Fork 760
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(IncrementNumberRounder): Implement RoundDouble Method
- Loading branch information
1 parent
9d6947b
commit 0fdab8e
Showing
3 changed files
with
98 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/Uno.UWP/Globalization/NumberFormatting/IncrementNumberRounder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Windows.Globalization.NumberFormatting | ||
{ | ||
public partial class IncrementNumberRounder : global::Windows.Globalization.NumberFormatting.INumberRounder | ||
{ | ||
private static readonly double[] Exceptions = new double[] | ||
{ | ||
1E-11, | ||
1E-12, | ||
1E-13, | ||
1E-14, | ||
1E-15, | ||
1E-16, | ||
1E-17, | ||
1E-18, | ||
1E-19, | ||
1E-20, | ||
}; | ||
|
||
private RoundingAlgorithm roundingAlgorithm = RoundingAlgorithm.RoundHalfUp; | ||
private double increment = 1d; | ||
|
||
public RoundingAlgorithm RoundingAlgorithm | ||
{ | ||
get => roundingAlgorithm; | ||
set | ||
{ | ||
if (value == RoundingAlgorithm.None) | ||
throw new ArgumentException("The parameter is incorrect"); | ||
|
||
roundingAlgorithm = value; | ||
} | ||
} | ||
|
||
public double Increment | ||
{ | ||
get => increment; | ||
set | ||
{ | ||
if (value <= 0) | ||
{ | ||
throw new ArgumentException("The parameter is incorrect"); | ||
} | ||
else if (value <= 0.5) | ||
{ | ||
if (!Exceptions.Any(e => e == value)) | ||
{ | ||
var inv = (1 / value); | ||
var n = Math.Truncate(inv); | ||
if (n < 2 || n > 10000000000) | ||
{ | ||
throw new ArgumentException("The parameter is incorrect"); | ||
} | ||
|
||
var modf = Math.Round(inv % 1, 14, MidpointRounding.AwayFromZero); | ||
if (modf > 0) | ||
{ | ||
throw new ArgumentException("The parameter is incorrect"); | ||
} | ||
} | ||
} | ||
else if (value < 1) | ||
{ | ||
throw new ArgumentException("The parameter is incorrect"); | ||
} | ||
else if (Math.Truncate(value) != value) | ||
{ | ||
throw new ArgumentException("The parameter is incorrect"); | ||
} | ||
|
||
|
||
increment = value; | ||
} | ||
} | ||
|
||
public IncrementNumberRounder() | ||
{ | ||
} | ||
|
||
public double RoundDouble(double value) | ||
{ | ||
var rounded = value / increment; | ||
rounded = Rounder.Round(rounded, 0, RoundingAlgorithm); | ||
rounded *= increment; | ||
|
||
return rounded; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters