I get the error: 'Input string was not in a correct format'..... I am running an if else calculation and then populating a label with the result the variable is declared as decimal then ToString('C') to the label...
List<string> moneyWon = new List<string>();
private void calculateAmountWon()
{
moneyWon.Add(amountWonlabel.Text);
decimal won = moneyWon.Sum(str => Convert.ToInt32(str));
moneyWonLabel.Text = won.ToString("C");
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
Based on what you're outputting I'm assuming that your strings are formatted as currency with a currency symbol and two decimal places. If that's the case you can change your parsing to:
decimal won = moneyWon.Sum(str => decimal.Parse(str, NumberStyles.Currency));
You're still vulnerable to invalid formats but if the values are all set programatically then they should be predictable.
Another option is to use a list of numeric types and parse up front:
List<decimal> moneyWon = new List<decimal>();
private void calculateAmountWon()
{
moneyWon.Add(decimal.Parse(amountWonlabel.Text, NumberStyles.Currency));
decimal won = moneyWon.Sum();
moneyWonLabel.Text = won.ToString("C");
}