/// <summary> /// This scenario illustrates how to provide a rounding algorithm to a CurrencyFormatter class by means of the /// applyRoundingForCurrency method. This will associate an IncrementNumberRounder using the same increment as the /// fractionDigits appropriate for the currency and language. You can always choose to provide a different rounding /// algorithm by setting the numberRounder property, as depicted in the doPaddingAndRoundingScenarios function. /// The mechanism provided here is better suited for currencies. /// </summary> /// <param name="currencyCode"></param> /// <param name="roundingAlgorithm"></param> private string DoCurrencyRoundingScenarios(string currencyCode, RoundingAlgorithm roundingAlgorithm) { // Create the currency formatter and set the rounding algorithm provided as a parameter CurrencyFormatter currencyFormatter = new Windows.Globalization.NumberFormatting.CurrencyFormatter(currencyCode); currencyFormatter.ApplyRoundingForCurrency(roundingAlgorithm); // Display the properties of the scenario StringBuilder results = new StringBuilder(); results.Append("Currency formatting for "); results.Append(currencyFormatter.Currency + " currency and using the "); results.Append(DisplayRoundingAlgorithmAsString(roundingAlgorithm) + " rounding algorithm\n\n"); // Iterate through the numbers to round and add them to the results double[] numbersToFormat = new double[] { 14.55, 3.345, 16.2, 175.8 }; foreach (double numberToFormat in numbersToFormat) { string formattedNumber = currencyFormatter.Format(numberToFormat); results.Append("Value: " + numberToFormat + " Formatted: " + formattedNumber + "\n"); } // Add a carriage return at the end of the scenario for readability results.AppendLine(); return(results.ToString()); }
private void When_UsingARoundingAlgorithmCore(double value, RoundingAlgorithm roundingAlgorithm, double expected) { var sut = new SignificantDigitsNumberRounder(); sut.SignificantDigits = 2; sut.RoundingAlgorithm = roundingAlgorithm; var rounded = sut.RoundDouble(value); Assert.AreEqual(expected, rounded); }
public void When_UsingARoundingAlgorithmCore(double value, RoundingAlgorithm roundingAlgorithm, double expected) { var sut = new IncrementNumberRounder(); sut.Increment = 0.25; sut.RoundingAlgorithm = roundingAlgorithm; var rounded = sut.RoundDouble(value); Assert.AreEqual(expected, rounded); }
/// <summary> /// Demonstrates how to perform padding and rounding by using the DecimalFormatter class (can be any of the /// formatter classes in the Windows.Globalization.Numberformatting namespace) and the IncrementNumberRounder /// to do so. The SignificantDigitsNumberRounder can also be used instead of the latter. /// </summary> /// <param name="roundingAlgorithm"></param> /// <param name="significantDigits"></param> /// <param name="integerDigits"></param> /// <param name="fractionalDigits"></param> string DoPaddingAndRoundingScenarios(RoundingAlgorithm roundingAlgorithm, int significantDigits, int integerDigits, int fractionalDigits) { // Create the rounder and set the rounding algorithm provided as a parameter IncrementNumberRounder rounder = new Windows.Globalization.NumberFormatting.IncrementNumberRounder(); rounder.RoundingAlgorithm = roundingAlgorithm; // Create the formatter, set the padding properties provided as parameters and associate the rounder // we have just created DecimalFormatter formatter = new Windows.Globalization.NumberFormatting.DecimalFormatter(); formatter.SignificantDigits = significantDigits; formatter.IntegerDigits = integerDigits; formatter.FractionDigits = fractionalDigits; formatter.NumberRounder = rounder; // Stores the results StringBuilder results = new StringBuilder(); // Iterate through the increments we have defined in the scenario double[] incrementsToRound = new double[] { 0.001, 0.01, 0.1, 1.0 }; foreach (double incrementToRound in incrementsToRound) { // Set the increment to round to rounder.Increment = incrementToRound; // Display the properties of the scenario results.Append("Padding and rounding with "); results.Append(formatter.SignificantDigits + " significant digits, "); results.Append(formatter.IntegerDigits + " integer digits, "); results.Append(formatter.FractionDigits + " fractional digits, "); results.Append(rounder.Increment + " increment and "); results.Append(DisplayRoundingAlgorithmAsString(rounder.RoundingAlgorithm) + " rounding algorithm\n\n"); // Iterate through the numbers to round and pad, format them and add them to the results double[] numbersToFormat = new double[] { 0.1458, 1.458, 14.58, 145.8 }; foreach (double numberToFormat in numbersToFormat) { string formattedNumber = formatter.Format(numberToFormat); results.Append("Value: " + numberToFormat + " Formatted: " + formattedNumber + "\n"); } // Add a carriage return at the end of the scenario for readability results.AppendLine(); } return(results.ToString()); }
/// <summary> /// Shows how to do number rounding using the IncrementNumberRounding class. ///</summary> ///<param name="roundingAlgorithm"></param> private string DoIncrementRoundingScenarios(RoundingAlgorithm roundingAlgorithm) { // Create the rounder and set the rounding algorithm provided as a parameter IncrementNumberRounder rounder = new Windows.Globalization.NumberFormatting.IncrementNumberRounder(); rounder.RoundingAlgorithm = roundingAlgorithm; // Formatter to display the rounded value. It is recommended to use the increment number // rounder within a formatter object to avoid precision issues when displaying. DecimalFormatter formatter = new Windows.Globalization.NumberFormatting.DecimalFormatter(); formatter.NumberRounder = rounder; // Stores the results StringBuilder results = new StringBuilder(); // Iterate through the increments we have defined in the scenario double[] incrementsToRound = new double[] { 0.001, 0.01, 0.1, 1.0 }; foreach (double incrementToRound in incrementsToRound) { // Set the significant digits to round to rounder.Increment = incrementToRound; // Display the properties of the scenario results.Append("Rounding with "); results.Append(rounder.Increment + " increment and "); results.Append(DisplayRoundingAlgorithmAsString(rounder.RoundingAlgorithm) + " rounding algorithm\n\n"); // Iterate through the numbers to round and add them to the results double[] numbersToRound = new double[] { 0.1458, 1.458, 14.58, 145.8 }; foreach (double numberToRound in numbersToRound) { results.Append("Value: " + numberToRound + " Rounded: " + formatter.Format(numberToRound) + "\n"); } // Add a carriage return at the end of the scenario for readability results.AppendLine(); } return(results.ToString()); }
/// <summary> /// Shows how to do number rounding using the SignificantDigitsNumberRounder class. /// </summary> /// <param name="roundingAlgorithm"></param> private string DoSignificantDigitRoundingScenarios(RoundingAlgorithm roundingAlgorithm) { // Create the rounder and set the rounding algorithm provided as a parameter. SignificantDigitsNumberRounder rounder = new Windows.Globalization.NumberFormatting.SignificantDigitsNumberRounder(); rounder.RoundingAlgorithm = roundingAlgorithm; // Stores the results StringBuilder results = new StringBuilder(); // Iterate through the significant digits we have defined in the scenario uint[] digitsToRound = new uint[] { 3, 2, 1 }; foreach (uint digitToRound in digitsToRound) { // Set the significant digits to round to rounder.SignificantDigits = digitToRound; // Display the properties of the scenario results.Append("Rounding with "); results.Append(rounder.SignificantDigits + " significant digits and "); results.Append(DisplayRoundingAlgorithmAsString(rounder.RoundingAlgorithm) + " rounding algorithm\n\n"); // Iterate through the numbers to round and add them to the results double[] numbersToRound = new double[] { 0.1458, 1.458, 14.58, 145.8 }; foreach (double numberToRound in numbersToRound) { double roundedValue = rounder.RoundDouble(numberToRound); results.Append("Value: " + numberToRound + " Rounded: " + roundedValue + "\n"); } // Add a carriage return at the end of the scenario for readability results.AppendLine(); } return(results.ToString()); }
/// <summary> /// Shows how to do number rounding using the SignificantDigitsNumberRounder class. /// </summary> /// <param name="roundingAlgorithm"></param> private string DoSignificantDigitRoundingScenarios(RoundingAlgorithm roundingAlgorithm) { // Create the rounder and set the rounding algorithm provided as a parameter. SignificantDigitsNumberRounder rounder = new Windows.Globalization.NumberFormatting.SignificantDigitsNumberRounder(); rounder.RoundingAlgorithm = roundingAlgorithm; // Stores the results StringBuilder results = new StringBuilder(); // Iterate through the significant digits we have defined in the scenario uint[] digitsToRound = new uint[] { 3, 2, 1 }; foreach (uint digitToRound in digitsToRound) { // Set the significant digits to round to rounder.SignificantDigits = digitToRound; // Display the properties of the scenario results.Append("Rounding with "); results.Append(rounder.SignificantDigits + " significant digits and "); results.Append(DisplayRoundingAlgorithmAsString(rounder.RoundingAlgorithm) + " rounding algorithm\n\n"); // Iterate through the numbers to round and add them to the results double[] numbersToRound = new double[] { 0.1458, 1.458, 14.58, 145.8 }; foreach (double numberToRound in numbersToRound) { double roundedValue = rounder.RoundDouble(numberToRound); results.Append("Value: " + numberToRound + " Rounded: " + roundedValue + "\n"); } // Add a carriage return at the end of the scenario for readability results.AppendLine(); } return results.ToString(); }
/// <summary> /// Shows how to do number rounding using the IncrementNumberRounding class. ///</summary> ///<param name="roundingAlgorithm"></param> private string DoIncrementRoundingScenarios(RoundingAlgorithm roundingAlgorithm) { // Create the rounder and set the rounding algorithm provided as a parameter IncrementNumberRounder rounder = new Windows.Globalization.NumberFormatting.IncrementNumberRounder(); rounder.RoundingAlgorithm = roundingAlgorithm; // Formatter to display the rounded value. It is recommended to use the increment number // rounder within a formatter object to avoid precision issues when displaying. DecimalFormatter formatter = new Windows.Globalization.NumberFormatting.DecimalFormatter(); formatter.NumberRounder = rounder; // Stores the results StringBuilder results = new StringBuilder(); // Iterate through the increments we have defined in the scenario double[] incrementsToRound = new double[] { 0.001, 0.01, 0.1, 1.0 }; foreach (double incrementToRound in incrementsToRound) { // Set the significant digits to round to rounder.Increment = incrementToRound; // Display the properties of the scenario results.Append("Rounding with "); results.Append(rounder.Increment + " increment and "); results.Append(DisplayRoundingAlgorithmAsString(rounder.RoundingAlgorithm) + " rounding algorithm\n\n"); // Iterate through the numbers to round and add them to the results double[] numbersToRound = new double[] { 0.1458, 1.458, 14.58, 145.8 }; foreach (double numberToRound in numbersToRound) { results.Append("Value: " + numberToRound + " Rounded: " + formatter.Format(numberToRound) + "\n"); } // Add a carriage return at the end of the scenario for readability results.AppendLine(); } return results.ToString(); }
/// <summary> /// This scenario illustrates how to provide a rounding algorithm to a CurrencyFormatter class by means of the /// applyRoundingForCurrency method. This will associate an IncrementNumberRounder using the same increment as the /// fractionDigits appropriate for the currency and language. You can always choose to provide a different rounding /// algorithm by setting the numberRounder property, as depicted in the doPaddingAndRoundingScenarios function. /// The mechanism provided here is better suited for currencies. /// </summary> /// <param name="currencyCode"></param> /// <param name="roundingAlgorithm"></param> private string DoCurrencyRoundingScenarios(string currencyCode, RoundingAlgorithm roundingAlgorithm) { // Create the currency formatter and set the rounding algorithm provided as a parameter CurrencyFormatter currencyFormatter = new Windows.Globalization.NumberFormatting.CurrencyFormatter(currencyCode); currencyFormatter.ApplyRoundingForCurrency(roundingAlgorithm); // Display the properties of the scenario StringBuilder results = new StringBuilder(); results.Append("Currency formatting for "); results.Append(currencyFormatter.Currency + " currency and using the "); results.Append(DisplayRoundingAlgorithmAsString(roundingAlgorithm) + " rounding algorithm\n\n"); // Iterate through the numbers to round and add them to the results double[] numbersToFormat = new double[] { 14.55, 3.345, 16.2, 175.8 }; foreach (double numberToFormat in numbersToFormat) { string formattedNumber = currencyFormatter.Format(numberToFormat); results.Append("Value: " + numberToFormat + " Formatted: " + formattedNumber + "\n"); } // Add a carriage return at the end of the scenario for readability results.AppendLine(); return results.ToString(); }
/// <summary> /// Demonstrates how to perform padding and rounding by using the DecimalFormatter class (can be any of the /// formatter classes in the Windows.Globalization.Numberformatting namespace) and the IncrementNumberRounder /// to do so. The SignificantDigitsNumberRounder can also be used instead of the latter. /// </summary> /// <param name="roundingAlgorithm"></param> /// <param name="significantDigits"></param> /// <param name="integerDigits"></param> /// <param name="fractionalDigits"></param> string DoPaddingAndRoundingScenarios(RoundingAlgorithm roundingAlgorithm, int significantDigits, int integerDigits, int fractionalDigits) { // Create the rounder and set the rounding algorithm provided as a parameter IncrementNumberRounder rounder = new Windows.Globalization.NumberFormatting.IncrementNumberRounder(); rounder.RoundingAlgorithm = roundingAlgorithm; // Create the formatter, set the padding properties provided as parameters and associate the rounder // we have just created DecimalFormatter formatter = new Windows.Globalization.NumberFormatting.DecimalFormatter(); formatter.SignificantDigits = significantDigits; formatter.IntegerDigits = integerDigits; formatter.FractionDigits = fractionalDigits; formatter.NumberRounder = rounder; // Stores the results StringBuilder results = new StringBuilder(); // Iterate through the increments we have defined in the scenario double[] incrementsToRound = new double[] { 0.001, 0.01, 0.1, 1.0 }; foreach (double incrementToRound in incrementsToRound) { // Set the increment to round to rounder.Increment = incrementToRound; // Display the properties of the scenario results.Append("Padding and rounding with "); results.Append(formatter.SignificantDigits + " significant digits, "); results.Append(formatter.IntegerDigits + " integer digits, "); results.Append(formatter.FractionDigits + " fractional digits, "); results.Append(rounder.Increment + " increment and "); results.Append(DisplayRoundingAlgorithmAsString(rounder.RoundingAlgorithm) + " rounding algorithm\n\n"); // Iterate through the numbers to round and pad, format them and add them to the results double[] numbersToFormat = new double[] {0.1458, 1.458, 14.58, 145.8 }; foreach (double numberToFormat in numbersToFormat) { string formattedNumber = formatter.Format(numberToFormat); results.Append("Value: " + numberToFormat + " Formatted: " + formattedNumber + "\n"); } // Add a carriage return at the end of the scenario for readability results.AppendLine(); } return results.ToString(); }
private void When_UsingARoundingAlgorithmCore(double value, RoundingAlgorithm roundingAlgorithm, double expected) { var rounded = Rounder.Round(value, 1, roundingAlgorithm); Assert.AreEqual(expected, rounded); }
public static double Round(double value, int digits, RoundingAlgorithm roundingAlgorithm) { var pow10 = Math.Pow(10, digits); value *= pow10; switch (roundingAlgorithm) { case RoundingAlgorithm.RoundDown: value = Math.Floor(value); break; case RoundingAlgorithm.RoundUp: value = Math.Ceiling(value); break; case RoundingAlgorithm.RoundTowardsZero: { if (value > 0) { value = Round(value, 0, RoundingAlgorithm.RoundDown); } else { value = Round(value, 0, RoundingAlgorithm.RoundUp); } } break; case RoundingAlgorithm.RoundAwayFromZero: { if (value > 0) { value = Round(value, 0, RoundingAlgorithm.RoundUp); } else { value = Round(value, 0, RoundingAlgorithm.RoundDown); } } break; case RoundingAlgorithm.RoundHalfDown: { var isHalf = IsFractionExactlyHalf(value); if (isHalf) { value = Round(value, 0, RoundingAlgorithm.RoundDown); } else { value = Math.Round(value, 0, MidpointRounding.AwayFromZero); } } break; case RoundingAlgorithm.RoundHalfUp: { var isHalf = IsFractionExactlyHalf(value); if (isHalf) { value = Round(value, 0, RoundingAlgorithm.RoundUp); } else { value = Math.Round(value, 0, MidpointRounding.AwayFromZero); } } break; case RoundingAlgorithm.RoundHalfTowardsZero: { var isHalf = IsFractionExactlyHalf(value); if (isHalf) { value = Round(value, 0, RoundingAlgorithm.RoundTowardsZero); } else { value = Math.Round(value, 0, MidpointRounding.AwayFromZero); } } break; case RoundingAlgorithm.RoundHalfAwayFromZero: { var isHalf = IsFractionExactlyHalf(value); if (isHalf) { value = Round(value, 0, RoundingAlgorithm.RoundAwayFromZero); } else { value = Math.Round(value, 0, MidpointRounding.AwayFromZero); } } break; case RoundingAlgorithm.RoundHalfToEven: value = Math.Round(value, 0, MidpointRounding.ToEven); break; case RoundingAlgorithm.RoundHalfToOdd: { var intPart = Math.Truncate(value); var isHalf = IsFractionExactlyHalf(value); if (isHalf) { if ((intPart % 2 == 1 && value > 0) || (intPart % 2 == 0 && value < 0)) { value = Math.Floor(value); } else { value = Math.Ceiling(value); } } else { value = Math.Round(value, 0, MidpointRounding.AwayFromZero); } } break; default: value = Math.Round(value, 0, MidpointRounding.AwayFromZero); break; } value /= pow10; return(value); }