Double Variables in C# .NET

<< Continues from the previous lesson

Add another button to your form, and set the following properties for it in the Properties Window:

Name: btnDouble
Location: 110, 130
Text: Double

Double click your new button to get at the code. Add the following three lines to your button code:

double myDouble;

myDouble = 0.007;

MessageBox.Show(myDouble.ToString());

Your coding window should now look like this:

Your coding window

Run your programme and click your new button. You should see this:

Example of Double variable type

You also need to be careful of precision when using double variable types. The double type can hold up to 16 digits.

Halt your programme and return to the coding window. Change this line:

myDouble = 0.007;

to this:

myDouble = 12345678.1234567;

Run your programme and click your double button. The message box correctly displays the number. Add another number on the end, though, and C# will again round up or down. The moral is, if you want accuracy, careful of rounding!

In the next part, you'll see how to add up in C#.