From 504f833b7ee713af24f72db002e4441081879f84 Mon Sep 17 00:00:00 2001 From: brianlagunas_cp Date: Tue, 5 Apr 2011 14:29:40 +0000 Subject: [PATCH] Calculator: localize value --- .../Calculator/Implementation/Calculator.cs | 10 ++++++---- .../Core/Utilities/CalculatorUtilities.cs | 10 ++++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Calculator/Implementation/Calculator.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Calculator/Implementation/Calculator.cs index 3e592d99..90f23f83 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Calculator/Implementation/Calculator.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Calculator/Implementation/Calculator.cs @@ -269,7 +269,7 @@ namespace Microsoft.Windows.Controls private decimal CalculateValue(Operation operation) { decimal newValue = decimal.Zero; - decimal currentValue = Decimal.Parse(DisplayText); + decimal currentValue = CalculatorUtilities.ParseDecimal(DisplayText); switch (operation) { @@ -345,10 +345,12 @@ namespace Microsoft.Windows.Controls private void ProcessMemoryKey(Calculator.CalculatorButtonType buttonType) { + decimal currentValue = CalculatorUtilities.ParseDecimal(DisplayText); + switch (buttonType) { case Calculator.CalculatorButtonType.MAdd: - Memory += Decimal.Parse(DisplayText); + Memory += currentValue; break; case Calculator.CalculatorButtonType.MC: Memory = decimal.Zero; @@ -357,10 +359,10 @@ namespace Microsoft.Windows.Controls DisplayText = Memory.ToString(); break; case Calculator.CalculatorButtonType.MS: - Memory = Decimal.Parse(DisplayText); + Memory = currentValue; break; case Calculator.CalculatorButtonType.MSub: - Memory -= Decimal.Parse(DisplayText); + Memory -= currentValue; break; default: break; diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Utilities/CalculatorUtilities.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Utilities/CalculatorUtilities.cs index 562aa3aa..9be880b3 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Utilities/CalculatorUtilities.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Utilities/CalculatorUtilities.cs @@ -29,9 +29,12 @@ namespace Microsoft.Windows.Controls.Core.Utilities case "\b": return Calculator.CalculatorButtonType.Back; case "\r": case "=": return Calculator.CalculatorButtonType.Equal; - case ".": return Calculator.CalculatorButtonType.Decimal; } + //the check for the decimal is not in the switch statement. To help localize we check against the current culture's decimal seperator + if (text == System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator) + return Calculator.CalculatorButtonType.Decimal; + //check for the escape key if (text == ((char)27).ToString()) return Calculator.CalculatorButtonType.Clear; @@ -193,7 +196,10 @@ namespace Microsoft.Windows.Controls.Core.Utilities } } - //TODO: add error handling + public static decimal ParseDecimal(string text) + { + return Decimal.Parse(text, System.Threading.Thread.CurrentThread.CurrentCulture); + } public static decimal Add(decimal firstNumber, decimal secondNumber) {