Calculator not returning decimal values?
My calculator is not returning decimal values for example if I do 2.2 + 2.2 the answer it returns is 44 instead of 4.4??
What did I do wrong???
Here's my Code
HTML Code:
namespace CalcW
{
public class Calculator
{
public decimal CurrentValue { get; set;}
private decimal First{get;set;}
private decimal Second{get;set;}
public void EnterValue(decimal value, bool n )
{
if (n)
{
First = value;
}
else
{
Second=value;
}
}
public void Add()
{
CurrentValue = First + Second;
//CurrentValue + value;
}
public void Subtract()
{
CurrentValue = First - Second;
//CurrentValue - value;
}
public void Multiple()
{
CurrentValue = First * Second;
//CurrentValue * value;
}
public void Divide()
{
CurrentValue =First/Second;
//CurrentValue /= value;
}
public decimal Equals(decimal value, string sn)
{
this.EnterValue(value, false);
switch(sn)
{
case "+/-": Reciprocal(); break;
case "+": Add(); break;
case "-": Subtract(); break;
case "*": Multiple(); break;
case "/": Divide(); break;
case "sqrt": SquareRoot(); break;
case "1/x": Fraction(); break;
}
First = 0;
Second = 0;
return CurrentValue;
}
public void Reciprocal()
{
CurrentValue =First *(-1);
}
public void SquareRoot()
{
}
public void Clear()
{
CurrentValue = 0;
}
public void Fraction()
{
CurrentValue = 1/First;
}
}
}
Re: Calculator not returning decimal values?
Quote:
Originally Posted by
QuestionPlease
if I do 2.2 + 2.2 the answer it returns is 44 instead of 4.4??
Then you're doing it wrong but as we can't see what you're actually doing, we can't say what's wrong with it. You must have some code that actually uses that class and that's where the problem will be.
Re: Calculator not returning decimal values?
Here is the Code that uses the Class.
Code:
namespace CalcW
{
public partial class Form1 : Form
{
private Calculator Calc { get; set; }
private string Sn { get; set; }
public Form1()
{
InitializeComponent();
Calc = new Calculator();
}
private void button13_Click(object sender, EventArgs e)
{
if (sender is Button)
{
if (TBScreen.Text == "0")
{
TBScreen.Text = "";
}
TBScreen.Text += ((Button) sender).Text.Trim('&');
}
}
private void button22_Click(object sender, EventArgs e)
{
TBScreen.Text = TBScreen.Text.Remove(TBScreen.Text.Length-1);
if (TBScreen.Text == string.Empty)
TBScreen.Text = "0";
}
private void button21_Click(object sender, EventArgs e)
{
TBScreen.Text = "0";
}
private void button3_Click(object sender, EventArgs e)
{
if (sender is Button)
{
Sn = ((Button)sender).Text.Trim('&');
Calc.EnterValue(Convert.ToDecimal(TBScreen.Text),true);
TBScreen.Text = "0";
}
}
private void button4_Click(object sender, EventArgs e)
{
if (!TBScreen.Text.Contains(','))
TBScreen.Text += ",";
}
private void button19_Click(object sender, EventArgs e)
{
if (Sn != String.Empty)
{
Calc.Equals(Convert.ToDecimal(TBScreen.Text), Sn);
TBScreen.Text = Calc.CurrentValue.ToString();
Sn = string.Empty;
}
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_KeyPress_1(object sender, KeyPressEventArgs e)
{
}
private void Form1_KeyPress_2(object sender, KeyPressEventArgs e)
{
if (e.KeyChar=='e')
{
if (Sn != String.Empty)
{
Calc.Equals(Convert.ToDecimal(TBScreen.Text), Sn);
TBScreen.Text = Calc.CurrentValue.ToString();
Sn = string.Empty;
}
}
}
}
}