/// <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> /// This is the click handler for the 'Display' button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Display_Click(object sender, RoutedEventArgs e) { // This scenario uses language tags with unicode extensions to construct and use the various // formatters and NumeralSystemTranslator in Windows.Globalization.NumberFormatting to format numbers // Keep results of the scenario in a StringBuilder StringBuilder results = new StringBuilder(); // Create number to format. double randomNumber = (new Random().NextDouble() * 100000); results.AppendLine("Random number used by formatters: " + randomNumber); // Create a string to translate String stringToTranslate = "These are the 10 digits of a numeral system: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9"; results.AppendLine("String used by NumeralSystemTranslater: " + stringToTranslate); // Create the rounder and set its increment to 0.01 IncrementNumberRounder rounder = new Windows.Globalization.NumberFormatting.IncrementNumberRounder(); rounder.Increment = 0.001; results.AppendLine("CurrencyFormatter will be using Euro symbol and all formatters rounding to 0.001 increments"); results.AppendLine(); // The list of language tags with unicode extensions we want to test String[] languages = new[] { "de-DE-u-nu-telu-ca-buddist-co-phonebk-cu-usd", "ja-jp-u-nu-arab"}; // Create the various formatters, now using the language list with unicode extensions results.AppendLine("Creating formatters using following languages in the language list:"); foreach(String language in languages) { results.AppendLine("\t" + language); } results.AppendLine(); // Create the various formatters. DecimalFormatter decimalFormatter = new DecimalFormatter(languages, "ZZ"); decimalFormatter.NumberRounder = rounder; decimalFormatter.FractionDigits = 2; CurrencyFormatter currencyFormatter = new CurrencyFormatter(CurrencyIdentifiers.EUR, languages, "ZZ"); currencyFormatter.NumberRounder = rounder; currencyFormatter.FractionDigits = 2; PercentFormatter percentFormatter = new PercentFormatter(languages, "ZZ"); percentFormatter.NumberRounder = rounder; percentFormatter.FractionDigits = 2; PermilleFormatter permilleFormatter = new PermilleFormatter(languages, "ZZ"); permilleFormatter.NumberRounder = rounder; permilleFormatter.FractionDigits = 2; NumeralSystemTranslator numeralTranslator = new NumeralSystemTranslator(languages); results.AppendLine("Using resolved language " + decimalFormatter.ResolvedLanguage + " : (note that all extension tags other than nu are ignored)"); // Format using formatters and translate using NumeralSystemTranslator. results.AppendLine("Decimal Formatted: " + decimalFormatter.Format(randomNumber)); results.AppendLine("Currency formatted: " + currencyFormatter.Format(randomNumber)); results.AppendLine("Percent formatted: " + percentFormatter.Format(randomNumber)); results.AppendLine("Permille formatted: " + permilleFormatter.Format(randomNumber)); results.AppendLine("NumeralTranslator translated: " + numeralTranslator.TranslateNumerals(stringToTranslate)); results.AppendLine(); // Display the results OutputTextBlock.Text = results.ToString(); }
/// <summary> /// This is the click handler for the 'Display' button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Display_Click(object sender, RoutedEventArgs e) { // This scenario uses the Windows.Globalization.NumberFormatting.CurrencyFormatter class // to format a number as a currency. // Keep results of the scenario in a StringBuilder StringBuilder results = new StringBuilder(); // Determine the current user's default currency. string currency = GlobalizationPreferences.Currencies[0]; // Generate numbers used for formatting. ulong wholeNumber = 12345; double fractionalNumber = 12345.67; // Create currency formatter initialized with current number formatting preferences. CurrencyFormatter defaultCurrencyFormatter = new CurrencyFormatter(currency); CurrencyFormatter usdCurrencyFormatter = new CurrencyFormatter(CurrencyIdentifiers.USD); CurrencyFormatter eurFRCurrencyFormatter = new CurrencyFormatter(CurrencyIdentifiers.EUR, new[] { "fr-FR" }, "FR"); CurrencyFormatter eurIECurrencyFormatter = new CurrencyFormatter(CurrencyIdentifiers.EUR, new[] { "gd-IE" }, "IE"); CurrencyFormatter currencyFormatEuroModeSwitch = new CurrencyFormatter(CurrencyIdentifiers.EUR); // Format numbers as currency. results.AppendLine("Fixed number (" + fractionalNumber + ")"); results.AppendLine("With user's default currency: " + defaultCurrencyFormatter.Format(fractionalNumber)); results.AppendLine("Formatted US Dollar: " + usdCurrencyFormatter.Format(fractionalNumber)); results.AppendLine("Formatted Euro (fr-FR defaults): " + eurFRCurrencyFormatter.Format(fractionalNumber)); results.AppendLine("Formatted Euro (gd-IE defaults): " + eurIECurrencyFormatter.Format(fractionalNumber)); // Format currency with fraction digits always included. usdCurrencyFormatter.FractionDigits = 2; results.AppendLine("Formatted US Dollar (with fractional digits): " + usdCurrencyFormatter.Format(wholeNumber)); // Format currenccy with grouping. usdCurrencyFormatter.IsGrouped = true; results.AppendLine("Formatted US Dollar (with grouping separators): " + usdCurrencyFormatter.Format(fractionalNumber)); // Format using currency code instead of currency symbol currencyFormatEuroModeSwitch.Mode = CurrencyFormatterMode.UseCurrencyCode; results.AppendLine("Formatted Euro (as currency code): " + currencyFormatEuroModeSwitch.Format(fractionalNumber)); // Switch so we can now format using currency symbol currencyFormatEuroModeSwitch.Mode = CurrencyFormatterMode.UseSymbol; results.AppendLine("Formatted Euro (as symbol): " + currencyFormatEuroModeSwitch.Format(fractionalNumber)); // Display the results OutputTextBlock.Text = results.ToString(); }
public ShoppingCartItemViewModel(ShoppingCartItem shoppingCartItem) { if (shoppingCartItem == null) { throw new ArgumentNullException("shoppingCartItem", "shoppingCartItem cannot be null"); } _id = shoppingCartItem.Id; _title = shoppingCartItem.Product.Title; _description = shoppingCartItem.Product.Description; _quantity = shoppingCartItem.Quantity; _listPrice = shoppingCartItem.Product.ListPrice; _imageUri = shoppingCartItem.Product.ImageUri; EntityId = shoppingCartItem.Id; ProductId = shoppingCartItem.Product.ProductNumber; _currencyFormatter = new CurrencyFormatter(shoppingCartItem.Currency); }
/// <summary> /// This is the click handler for the 'Display' button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Display_Click(object sender, RoutedEventArgs e) { // This scenario uses the Windows.Globalization.NumberFormatting.DecimalFormatter, // Windows.Globalization.NumberFormatting.CurrencyFormatter and // Windows.Globalization.NumberFormatting.PercentFormatter classes to format and parse a number // percentage or currency. // Keep results of the scenario in a StringBuilder StringBuilder results = new StringBuilder(); // Define percent formatters. PercentFormatter percentFormat = new PercentFormatter(); PercentFormatter percentFormatJP = new PercentFormatter(new string[] { "ja" }, "ZZ"); PercentFormatter percentFormatFR = new PercentFormatter(new string[] { "fr-FR" }, "ZZ"); // Define decimal formatters. DecimalFormatter decimalFormat = new DecimalFormatter(); decimalFormat.IsGrouped = true; DecimalFormatter decimalFormatJP = new DecimalFormatter(new string[] { "ja" }, "ZZ"); decimalFormatJP.IsGrouped = true; DecimalFormatter decimalFormatFR = new DecimalFormatter(new string[] { "fr-FR" }, "ZZ"); decimalFormatFR.IsGrouped = true; // Define currency formatters string userCurrency = GlobalizationPreferences.Currencies[0]; CurrencyFormatter currencyFormat = new CurrencyFormatter(userCurrency); CurrencyFormatter currencyFormatJP = new CurrencyFormatter("JPY", new string[] { "ja" }, "ZZ"); CurrencyFormatter currencyFormatFR = new CurrencyFormatter("EUR", new string[] { "fr-FR" }, "ZZ"); // Generate numbers for parsing. double percentNumber = 0.523; double decimalNumber = 12345.67; double currencyNumber = 1234.56; // Roundtrip the percent numbers by formatting and parsing. String percent1 = percentFormat.Format(percentNumber); double percent1Parsed = percentFormat.ParseDouble(percent1).Value; String percent1JP = percentFormatJP.Format(percentNumber); double percent1JPParsed = percentFormatJP.ParseDouble(percent1JP).Value; String percent1FR = percentFormatFR.Format(percentNumber); double percent1FRParsed = percentFormatFR.ParseDouble(percent1FR).Value; // Generate the results for percent parsing. results.AppendLine("Percent parsing of " + percentNumber); results.AppendLine("Formatted for current user: "******" Parsed as current user: "******"Formatted for ja-JP: " + percent1JP + " Parsed for ja-JP: " + percent1JPParsed); results.AppendLine("Formatted for fr-FR: " + percent1FR + " Parsed for fr-FR: " + percent1FRParsed); results.AppendLine(); // Roundtrip the decimal numbers for formatting and parsing. String decimal1 = decimalFormat.Format(decimalNumber); double decimal1Parsed = decimalFormat.ParseDouble(decimal1).Value; String decimal1JP = decimalFormatJP.Format(decimalNumber); double decimal1JPParsed = decimalFormatJP.ParseDouble(decimal1JP).Value; String decimal1FR = decimalFormatFR.Format(decimalNumber); double decimal1FRParsed = decimalFormatFR.ParseDouble(decimal1FR).Value; // Generate the results for decimal parsing. results.AppendLine("Decimal parsing of " + decimalNumber); results.AppendLine("Formatted for current user: "******" Parsed as current user: "******"Formatted for ja-JP: " + decimal1JP + " Parsed for ja-JP: " + decimal1JPParsed); results.AppendLine("Formatted for fr-FR: " + decimal1FR + " Parsed for fr-FR: " + decimal1FRParsed); results.AppendLine(); // Roundtrip the currency numbers for formatting and parsing. String currency1 = currencyFormat.Format(currencyNumber); double currency1Parsed = currencyFormat.ParseDouble(currency1).Value; String currency1JP = currencyFormatJP.Format(currencyNumber); double currency1JPParsed = currencyFormatJP.ParseDouble(currency1JP).Value; String currency1FR = currencyFormatFR.Format(currencyNumber); double currency1FRParsed = currencyFormatFR.ParseDouble(currency1FR).Value; // Generate the results for decimal parsing. results.AppendLine("Currency parsing of " + currencyNumber); results.AppendLine("Formatted for current user: "******" Parsed as current user: "******"Formatted for ja-JP: " + currency1JP + " Parsed for ja-JP: " + currency1JPParsed); results.AppendLine("Formatted for fr-FR: " + currency1FR + " Parsed for fr-FR: " + currency1FRParsed); results.AppendLine(); // Display the results. OutputTextBlock.Text = 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(); }
private void UpdatePrices(double shippingCost) { var currencyFormatter = new CurrencyFormatter(_order.ShoppingCart.Currency); OrderSubtotal = currencyFormatter.FormatDouble(Math.Round(_order.ShoppingCart.TotalPrice, 2)); ShippingCost = currencyFormatter.FormatDouble(shippingCost); var taxCost = Math.Round((_order.ShoppingCart.TotalPrice + shippingCost) * _order.ShoppingCart.TaxRate, 2); TaxCost = currencyFormatter.FormatDouble(taxCost); var grandTotal = Math.Round(_order.ShoppingCart.TotalPrice + shippingCost + taxCost, 2); GrandTotal = currencyFormatter.FormatDouble(grandTotal); }
/// <summary> /// Expense.UpdateData() method /// Updates all data /// </summary> /// <returns> /// Returns a bool object. /// </returns> public async Task<bool> UpdateData() { try { while (App.Busy) { await Task.Delay(100); } App.Busy = true; BudgetTotal = 0; Cost = 0; foreach (var part in Parts) { Cost += part.Cost; if (part.IsBudgetIncluded && this.IsBudgetIncluded) BudgetTotal += part.Cost; } CurrencyFormatter currency = new CurrencyFormatter(Windows.System.UserProfile.GlobalizationPreferences.Currencies[0]); HumanCost = currency.Format(Cost); DateTimeFormatter datetime = new DateTimeFormatter("shortdate"); HumanTransactionDate = datetime.Format(TransactionDate); return true; } catch (Exception ex) { App.Telemetry.TrackException(ex); return false; } finally { App.Busy = false; } }
public async Task<bool> UpdateData() { try { while (App.Busy) { await Task.Delay(100); } App.Busy = true; CurrencyFormatter currency = new CurrencyFormatter(Windows.System.UserProfile.GlobalizationPreferences.Currencies[0]); HumanCost = currency.Format(Cost); ModifiedDate = DateTime.Now; return true; } catch (Exception ex) { App.Telemetry.TrackException(ex); return false; } finally { App.Busy = false; } }