Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

C# Math (Decimals) [Intermediate Level Tutorial]

Math In C#


Intro:
When working with multiplying, dividing, subtracting, numbers from textBox's, you must always parse it. If you do not know how to do this, please see my parsing tutorial.

When working with decimals in C# "45.5235" you must use a floating point or a double. The difference between the two is this:

Float is a floating point on the end, it can hold up to 7 digits. (32 bit)
Double can have up to 16 digits. (64 bit)

Getting Started:
Create two textbox's, and a button.

Inside of the button, insert the following:

Code
double num1 = double.Parse(textBox1.Text);
double num2 = double.Parse(textBox2.Text);

double answer = num1 + num2;

            MessageBox.Show(answer.ToString());


The first line creates a new double variable, and parses the value from the first textbox.

The second line creates a new double variable, and parses the value from the second textbox.

The third line sets up another double variable, that adds the first two number.

Finally, the last line displays the answer.

If you want to do something other than add you need to use one of the other mathematical operators:

Code
+ Adds
- Subtracts
/ Divides
* Multiplies


Spoiler (Click to View)

Conclusion:
Calculating in C# can be used for so many different things. Whether its balancing your check book, finding out how much money you will make in a year, or calculating packets!


Code:
[IMG]http://i54.[censored].com/6yz9zp.jpg[/IMG]

Code
private void button4_Click(object sender, EventArgs e)
        {
            double num1 = double.Parse(textBox1.Text);
            double num2 = double.Parse(textBox2.Text);

            double answer = num1 + num2;

            MessageBox.Show(answer.ToString());
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            double num1 = double.Parse(textBox1.Text);
            double num2 = double.Parse(textBox2.Text);

            double answer = num1 / num2;

            MessageBox.Show(answer.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            double num1 = double.Parse(textBox1.Text);
            double num2 = double.Parse(textBox2.Text);

            double answer = num1 * num2;

            MessageBox.Show(answer.ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            double num1 = double.Parse(textBox1.Text);
            double num2 = double.Parse(textBox2.Text);

            double answer = num1 - num2;

            MessageBox.Show(answer.ToString());
        }

Also, I do not recommend reusing variable names, I just did it to be lazy!
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
nice work but no comment here yet i just start reading one by one i like read ur all tut its really nice thanks for good work sharing here its easy to understand now
"Intermediate Level Tutorial"

i'm thinking it's more of a beginer's tutorial.

"Intermediate Level Tutorial"
would include most of the common number variables, int, float, double, byte, and so on...
and how they react with eachother...
like... how
Code
int num = 100;
float num2 = 1.34332f;

float result = num * num2;
float result2 = num * (int)num2;
float result3 = (float)num * num2;
yea no, how result1 and result2 are the same, and result3 is not.

yea just my opinion, its a beginer's tutorial.
(Nov 12, 2010 04:32 PM)RoXaN Wrote:  nice work but no comment here yet i just start reading one by one i like read ur all tut its really nice thanks for good work sharing here its easy to understand now

Thanks :D! I was planning on making more if they really did help people.
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by dummycartSep 9, 2021 03:38 AM 1 146 Sep 9, 2021 03:38 AM
Last Post: Lindros
[Python] by CiataLearnerDec 22, 2018 02:27 PM 1 212 Dec 22, 2018 02:27 PM
Last Post: notjapanese
[C] by Adam2017Dec 3, 2018 03:06 PM 2 167 Dec 3, 2018 03:06 PM
Last Post: Adam2017