Skip to content

Commit

Permalink
feat(IncrementNumberRounder): Implement RoundDouble Method
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadHadi2031 committed Sep 8, 2021
1 parent 9d6947b commit 0fdab8e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,11 @@
#pragma warning disable 114 // new keyword hiding
namespace Windows.Globalization.NumberFormatting
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented]
#endif
public partial class IncrementNumberRounder : global::Windows.Globalization.NumberFormatting.INumberRounder
#endif
public partial class IncrementNumberRounder : global::Windows.Globalization.NumberFormatting.INumberRounder
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public global::Windows.Globalization.NumberFormatting.RoundingAlgorithm RoundingAlgorithm
{
get
{
throw new global::System.NotImplementedException("The member RoundingAlgorithm IncrementNumberRounder.RoundingAlgorithm is not implemented in Uno.");
}
set
{
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.Globalization.NumberFormatting.IncrementNumberRounder", "RoundingAlgorithm IncrementNumberRounder.RoundingAlgorithm");
}
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public double Increment
{
get
{
throw new global::System.NotImplementedException("The member double IncrementNumberRounder.Increment is not implemented in Uno.");
}
set
{
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.Globalization.NumberFormatting.IncrementNumberRounder", "double IncrementNumberRounder.Increment");
}
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public IncrementNumberRounder()
{
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.Globalization.NumberFormatting.IncrementNumberRounder", "IncrementNumberRounder.IncrementNumberRounder()");
}
#endif
// Forced skipping of method Windows.Globalization.NumberFormatting.IncrementNumberRounder.IncrementNumberRounder()
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public int RoundInt32( int value)
Expand Down Expand Up @@ -78,13 +42,7 @@ public float RoundSingle( float value)
throw new global::System.NotImplementedException("The member float IncrementNumberRounder.RoundSingle(float value) is not implemented in Uno.");
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public double RoundDouble( double value)
{
throw new global::System.NotImplementedException("The member double IncrementNumberRounder.RoundDouble(double value) is not implemented in Uno.");
}
#endif

// Forced skipping of method Windows.Globalization.NumberFormatting.IncrementNumberRounder.RoundingAlgorithm.get
// Forced skipping of method Windows.Globalization.NumberFormatting.IncrementNumberRounder.RoundingAlgorithm.set
// Forced skipping of method Windows.Globalization.NumberFormatting.IncrementNumberRounder.Increment.get
Expand Down
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ public double RoundDouble(double value)
return Rounder.Round(value, (int)diffLength, RoundingAlgorithm);
}

private static int GetIntegerLength(int integerPart)
private static int GetIntegerLength(int input)
{
if (integerPart == 0)
if (input == 0)
{
return 1;
}

return (int)Math.Floor(Math.Log10(Math.Abs(integerPart))) + 1;
return (int)Math.Floor(Math.Log10(Math.Abs(input))) + 1;
}
}
}

0 comments on commit 0fdab8e

Please sign in to comment.