/// <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(); }