I am working on a calculator in C# and this project is going to end me. I just learned about classes and my extent of formatting decimals is basically using formatting codes ex: ("C") for currency.
Here is what the project asks for:
"The results of multipication should show no more decimal places than the result of the first number than in the second number. Example 55.5*89.68 = 4977.240" and then a few other restrictions on addition, subtraction, and so on.
Currently the way my program is coded everything looks fine accept for multiplication. I believe what is throwing of my answers is how I have coded my division method.
Here is the code starting from my operator buttons. I did not format my output until I got to Division because that was the only test data that did not run correctly. When I formatted Division it is now throwing off Multiplication. I am unsure how to make both Multiplication and Division happy.
private void btnAdd_Click(object sender, System.EventArgs e)
{
calc.Add(displayValue);
newValue = true;
decimalEntered = false;
displayValue = calc.CurrentValue;
txtDisplay.Text = displayValue.ToString();
}
private void btnSubtract_Click(object sender, System.EventArgs e)
{
calc.Subtract(displayValue);
newValue = true;
decimalEntered = false;
displayValue = calc.CurrentValue;
txtDisplay.Text = displayValue.ToString();
}
private void btnMultiply_Click(object sender, System.EventArgs e)
{
calc.Multiply(displayValue);
newValue = true;
decimalEntered = false;
displayValue = calc.CurrentValue;
txtDisplay.Text = displayValue.ToString();
}
private void btnDivide_Click(object sender, System.EventArgs e)
{
calc.Divide(displayValue);
newValue = true;
decimalEntered = false;
displayValue = calc.CurrentValue;
displayValue = Math.Round(displayValue, 8);
txtDisplay.Text = displayValue.ToString();
}
private void btnEquals_Click(object sender, System.EventArgs e)
{
try
{
if (newValue)
calc.Equals();
else
calc.Equals(displayValue);
displayValue = calc.CurrentValue;
displayValue = Math.Round(displayValue, 8);
txtDisplay.Text = displayValue.ToString();
newValue = true;
decimalEntered = false;
}
catch (DivideByZeroException)
{
displayValue = 0;
txtDisplay.Text = "Cannot divide by zero.";
newValue = true;
decimalEntered = false;
}
}
}
}
I also should note I tried adding the following code into my division statement instead of the way I have it above, and this gave me errors which is why I went back to the way I have it above.
calc.Divide(displayValue);
newValue = true;
decimalEntered = false;
displayValue = calc.CurrentValue;
txtDisplay.Text = displayValue.ToString("f8");