Hello!
I'm doing a calculator in c# and I have one problem.
I can't get the "%" button to work. I want it to work just like microsofts calculator and it would be great if annyone could help me! :)

Its the code for how to calculate it I need!

Recommended Answers

All 3 Replies

Hi Siimont, welcome on the daniweb site.
Have a look at this thread, in one of the posts it has a zip file with some code for a calculator. It is a simulation of a calculator I have at home.
It acts differently than the MS one but if you look at the code it must be somehow possible to convert this into the action an MS calculator does in case of a percentage calculation.
This is a piece of my code that handles the "function" buttons on my calculator, for further details look in the zip file, or ask a question.

private void FunctionBtn_Click(object sender, EventArgs e)
        {
            if (_CalculatorON)
            {
                Button FunctionBtn = sender as Button;

                switch (FunctionBtn.Text)
                {
                    case "sqrt":
                        _NUMBER = Math.Sqrt(Convert.ToDouble(this.DisplayTxB.Text));
                        this.DisplayTxB.Text = AdjustDisplayString(_NUMBER.ToString());
                        break;
                    case "%":
                        _NUMBER = _ACCUMULATOR * _NUMBER / 100;
                        Calculate();
                        this.DisplayTxB.Text = AdjustDisplayString(_ACCUMULATOR.ToString());
                        _NUMBER = 0.0;
                        _NewNumber = true;
                        break;
                    case "1/x":
                        _NUMBER = Convert.ToDouble(this.DisplayTxB.Text);
                        if (_NUMBER != 0.0) _NUMBER = 1 / _NUMBER;
                        this.DisplayTxB.Text = AdjustDisplayString(_NUMBER.ToString());                    
                        break;
                    default:
                        break;
                }
            }
        } //END FunctionBtn_Click

Succes!

commented: Success! +9

Thank you so much ddanbe!
This solved my problem :D

u want code for percentage calculator in adding of function C# so you can go first the application in calculator then change inner value's of the digit.

commented: SPAM! -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.