El error se encuentra al momento de asignar el resultado de la operación en el label resultLabel, debido a que no está aplicando el mismo formato utilizado cuando realiza los double.TryParse en los métodos equalButton_Click y OperationButton_Click. Para no tener en muchas partes del código la parte de new CultureInfo("en-US") sería bueno dejarlo como una variable global.
public partial class MainWindow : Window
{
CultureInfo cultureInfo = new CultureInfo("en-US");
SelectedOperator selectedOperator;
Double primerNumero;
Double segundoNumero;
public MainWindow()
{
InitializeComponent();
acButton.Click += AcButton_Click;
negativeButton.Click += NegativeButton_Click;
percentegeButton.Click += PercentegeButton_Click;
}
private void PercentegeButton_Click(object sender, RoutedEventArgs e)
{
if (double.TryParse(resultLabel.Content.ToString(), out double auxiliar))
{
resultLabel.Content = auxiliar / 100;
}
}
private void NegativeButton_Click(object sender, RoutedEventArgs e)
{
if (double.TryParse(resultLabel.Content.ToString(), out double auxiliar))
{
resultLabel.Content = auxiliar * -1;
}
}
private void AcButton_Click(object sender, RoutedEventArgs e)
{
resultLabel.Content = "0";
}
private void OperationButton_Click(object sender, RoutedEventArgs e)
{
if (double.TryParse(resultLabel.Content.ToString(), NumberStyles.Any, cultureInfo, out primerNumero))
{
resultLabel.Content = "0";
}
if (sender == asteriskButton)
selectedOperator = SelectedOperator.Multiplication;
if (sender == divisionButton)
selectedOperator = SelectedOperator.Division;
if (sender == plusButton)
selectedOperator = SelectedOperator.Addition;
if (sender == minusButton)
selectedOperator = SelectedOperator.Sustraction;
}
private void NumberButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString().Equals("0"))
{
resultLabel.Content = (sender as Button).Content.ToString();
}
else
{
resultLabel.Content = $"{resultLabel.Content}{(sender as Button).Content.ToString()}";
}
}
public enum SelectedOperator
{
Addition,
Sustraction,
Multiplication,
Division
}
private void equalButton_Click(object sender, RoutedEventArgs e)
{
if (double.TryParse(resultLabel.Content.ToString(), NumberStyles.Any, cultureInfo, out segundoNumero))
{
switch (selectedOperator)
{
case SelectedOperator.Addition:
resultLabel.Content = suma(primerNumero, segundoNumero).ToString(cultureInfo);
break;
case SelectedOperator.Division:
resultLabel.Content = (primerNumero / segundoNumero).ToString(cultureInfo);
break;
case SelectedOperator.Multiplication:
resultLabel.Content = (primerNumero * segundoNumero).ToString(cultureInfo);
break;
case SelectedOperator.Sustraction:
resultLabel.Content = (primerNumero - segundoNumero).ToString(cultureInfo);
break;
}
}
}
private void dotButton_Click(object sender, RoutedEventArgs e)
{
if (!resultLabel.Content.ToString().Contains("."))
{
resultLabel.Content = $"{resultLabel.Content}.";
}
}
private double suma(double n1, double n2)
{
return (n1 + n2);
}
}
Se podría forzar la utilización del IFormatProvider desde el constructor MainWindow() y se eliminaría la mayor parte de conversiones.
public partial class MainWindow : Window
{
SelectedOperator selectedOperator;
Double primerNumero;
Double segundoNumero;
public MainWindow()
{
InitializeComponent();
acButton.Click += AcButton_Click;
negativeButton.Click += NegativeButton_Click;
percentegeButton.Click += PercentegeButton_Click;
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
}
private void PercentegeButton_Click(object sender, RoutedEventArgs e)
{
if (double.TryParse(resultLabel.Content.ToString(), out double auxiliar))
{
resultLabel.Content = auxiliar / 100;
}
}
private void NegativeButton_Click(object sender, RoutedEventArgs e)
{
if (double.TryParse(resultLabel.Content.ToString(), out double auxiliar))
{
resultLabel.Content = auxiliar * -1;
}
}
private void AcButton_Click(object sender, RoutedEventArgs e)
{
resultLabel.Content = "0";
}
private void OperationButton_Click(object sender, RoutedEventArgs e)
{
primerNumero = Convert.ToDouble(resultLabel.Content);
resultLabel.Content = "0";
if (sender == asteriskButton)
selectedOperator = SelectedOperator.Multiplication;
if (sender == divisionButton)
selectedOperator = SelectedOperator.Division;
if (sender == plusButton)
selectedOperator = SelectedOperator.Addition;
if (sender == minusButton)
selectedOperator = SelectedOperator.Sustraction;
}
private void NumberButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString().Equals("0"))
{
resultLabel.Content = (sender as Button).Content.ToString();
}
else
{
resultLabel.Content = $"{resultLabel.Content}{(sender as Button).Content.ToString()}";
}
}
public enum SelectedOperator
{
Addition,
Sustraction,
Multiplication,
Division
}
private void equalButton_Click(object sender, RoutedEventArgs e)
{
segundoNumero = Convert.ToDouble(resultLabel.Content);
switch (selectedOperator)
{
case SelectedOperator.Addition:
resultLabel.Content = (primerNumero + segundoNumero).ToString();
break;
case SelectedOperator.Division:
resultLabel.Content = (primerNumero / segundoNumero).ToString();
break;
case SelectedOperator.Multiplication:
resultLabel.Content = (primerNumero * segundoNumero).ToString();
break;
case SelectedOperator.Sustraction:
resultLabel.Content = (primerNumero - segundoNumero).ToString();
break;
}
}
private void dotButton_Click(object sender, RoutedEventArgs e)
{
if (!resultLabel.Content.ToString().Contains("."))
{
resultLabel.Content = $"{resultLabel.Content}.";
}
}
}