3 Replies - 270 Views - Last Post: 05 December 2012 - 07:41 AM Rate Topic: -----

#1 HPLG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-November 12

Calculator Not Returning Decimal Values

Posted 04 December 2012 - 06:50 PM

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
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;
        }
       
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Calculator Not Returning Decimal Values

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,529
  • Joined: 05-May 12

Re: Calculator Not Returning Decimal Values

Posted 04 December 2012 - 06:57 PM

If you set a breakpoint on line 70, what is the value of CurrentValue?

On first glance, the code looks correct. I think the issue maybe with your display code rather than your computation.

This post has been edited by Skydiver: 04 December 2012 - 06:58 PM

Was This Post Helpful? 0
  • +
  • -

#3 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Calculator Not Returning Decimal Values

Posted 05 December 2012 - 07:24 AM

One logical thing I would like to point out here.

Reciprocal of a number is written as 1/(that number) and not number* (-1)

Eg.Reciprocal of 4 is 1/4

regards,
Raghav
Was This Post Helpful? 2
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,354
  • Joined: 02-June 10

Re: Calculator Not Returning Decimal Values

Posted 05 December 2012 - 07:41 AM

Calculator tutorial
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1