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