1 Replies - 268 Views - Last Post: 15 November 2011 - 09:17 AM Rate Topic: -----

#1 stefanere2k9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 15-November 11

problem with my calculator format decimal places

Posted 15 November 2011 - 08:18 AM

hey i have created a calculator i want to to press format button then for it to display enter decimal places and then i want to click on a button for e.g 1 to display 1 decimal places ect. the problem i am having is when i press 1 it comes up 0.0 and then i want to be able to make a calculation with 1 decimal place but it just doing 0 decimal places and say when i press cancel it just display as 0 decminal places but if i do a calculation such as 12 = 12 = 24 when i do the format button then press 1 it will come up as 24.0 and always if i press 2 it just comes to 2 decimal places. so am getting something wrong on the if statement i think here my code.

public partial class Form1 : Form
    {
        double total1 = 0;
        double total2 = 0;
        double operand1 = 0;
        double operand2 = 0;
        double opcount = 0;
        double myVar = 0;
        string theOperator;
        bool buttonPressed = false;
        Boolean clearTxt = true;

        public Form1()
        {
            InitializeComponent();
        }

                private void noOne_Click(object sender, EventArgs e)
        {
            if (clearTxt == true)
            {
                displayTxt.Clear();
                displayTxt.Text = "1";
                clearTxt = false;
            }
            else
            {
                displayTxt.Text = displayTxt.Text + "1";
            }
            backSpace.Enabled = true;
            if (buttonPressed == true)
            {
                double myVar = total2, total1, operand1, operand2;
                displayTxt.Text = myVar.ToString("N1");
                noDot.Enabled = true;
                noEqual.Enabled = true;
                noCancel.Enabled = true;
                noDivide.Enabled = true;
                noTimes.Enabled = true;
                noSubtract.Enabled = true;
                noPlus.Enabled = true;
                backSpace.Enabled = true;
                displayTxt.BackColor = Color.Black;
                displayTxt.ForeColor = Color.PaleGreen;
                formatDec.Text = ("format: 1 d.p.");
                clearTxt = true;
            }

and here my cancel button code

{
                    displayTxt.Clear();
                    displayTxt.Text = "0";
                    total1 = 0;
                    total2 = 0;
                    operand1 = 0;
                    operand2 = 0;
                    clearTxt = true;
                    noDot.Enabled = true;
                    backSpace.Enabled = false;
                }


mod edit: use code tags!
:code:

This post has been edited by Curtis Rutland: 15 November 2011 - 08:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: problem with my calculator format decimal places

#2 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: problem with my calculator format decimal places

Posted 15 November 2011 - 09:17 AM

If I'm reading this right you want to display 24.00 if you set it to display 2 decimal places, 24.000 if you set as 3 decimal places etc? If so then you will find that for a whole number the textbox will format the text to only display the decimals if there is some value as a remainder, and only to the correct percision. For example if you have 2 decimal places it will round 24.00567 as 24.01, but 24.1000 as 24.1. Does that make sense? I'm finding it hard to explain. You can convert the number to a sting and then add on the appropriate 0's before displaying it if you really want to display a number such as 24.00, but unless I'm mistaken the textbox will always format a number to cut off the unneeded .00 if it is displaying a number.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1