Monday 30 October 2017

floating point - Difference between decimal, float and double in .NET?

Integers, as was mentioned, are whole numbers. They can't
store the point something, like .7, .42, and .007. If you need to store numbers that are
not whole numbers, you need a different type of variable. You can use the double type or
the float type. You set these types of variables up in exactly the same way: instead of
using the word int, you type double or
float. Like
this:


float myFloat;
double
myDouble;

(float
is short for "floating point", and just means a number with a point something on the
end.)


The difference between the two is in the size of the
numbers that they can hold. For float, you can have up to 7
digits in your number. For doubles, you can have up to 16
digits. To be more precise, here's the official
size:


float: 1.5 × 10^-45 to 3.4 ×
10^38
double: 5.0 × 10^-324 to 1.7 ×
10^308

float
is a 32-bit number, and double is a 64-bit
number.


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());

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


myDouble = 0.007;
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, be careful of rounding!

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...