- Back to Home »
- C# Tutorial »
- Double Variables in C# .NET
Posted by : senan
Friday, February 14, 2014
Add another button to your form, and set the following properties
for it in the Properties Window:
Name: btnDouble
Location: 110, 130
Text: Double
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:
Run your programme and click your new button. You should see this:
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#.