按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
UShort Unsigned 16…bit integer; the smallest value is 0; and the largest value is 65535
Short Signed 16…bit integer; the smallest value is –32768; and the largest value is 32767
UInteger Unsigned 32…bit integer; the smallest value is 0; and the largest value is
4294967295
Integer Signed 32…bit integer; the smallest value is –2147483648; and the largest value is
2147483647
ULong Unsigned 64…bit integer; the smallest value is 0; and the largest value is
18446744073709551615
Long Signed 64…bit integer; the smallest value is –9223372036854775808; and the
largest value is 9223372036854775807
Single 32…bit floating…point number; the smallest value is –3。4x1038; and the largest
value is 3。4x1038; with a precision of 7 digits
Double 64…bit floating…point number; the smallest value is –1。7x10308; and the largest
value is 1。7x10308; with 15 to 16 digits of precision
Decimal Special 128…bit data type; the smallest value is 1。0x10–28; and the largest value is
1。0x1028; with at least 28 significant digits of precisiona
a The Decimal type is often used for financial data because sometimes a calculation will result in one penny
less than the correct result (for example; 14。9999; instead of 15。00) due to rounding errors。
…………………………………………………………Page 67……………………………………………………………
CH A PT E R 2 ■ L E A R N I N G A B OU T 。 N E T N U M B E R AN D V A L U E T Y P E S 45
With so many variations of number types available; you may be wondering which ones to
use and when。 The quick answer is that it depends on your needs。 When performing scientific
calculations; you probably need to use a Double or Single。 If you are calculating mortgages; you
probably need to use a Decimal。 And if you are performing set calculations; you probably should
use an Integer or a Long。 It all depends on how accurate you want to be; or how much numeric
precision you want。
Numeric precision is an important topic and should never be dealt with lightly。 Consider
the following example: every country takes a census of its people; and when the census is
piled; we learn some interesting facts。 For example; in Canada; 31% of people will divorce。
Canada has a population clock that says every minute and 32 seconds; someone is born。 At the
time of this writing; the population was 32;789;736。 Thus; at the time of this writing; 10;164;818
people will divorce。 Think a bit about what I just wrote。 I said that there is a direct relationship
of people who will divorce to the number of births in Canada (31%; in fact)。 You should be
amazed that the births and divorces are timed to the point where 10;164;818—not 10;164;819
nor 10;164;820—people will divorce。 Of course; I’m being cynical and just trying to make the
point that numbers are just that: numbers that you round off。
I can’t say 10;164;818 people will divorce; because I can’t be that accurate without performing
an actual count。 I could probably say 10;164;818 plus or minus 100;000 will divorce。 Using the
plus or minus; the range is 10;064;818 to 10;264;818; or roughly speaking; 10。2 million people。
The number 10。2 million is what a newsperson would report; what literature would say; and
what most people would use in conversation。 So; if I add 10。2 million and 1;000; can I say that
the total is 10;201;000? The 10。2 is a roundoff to the nearest tenth of a million; and adding a
thousand means adding a number that is less than the roundoff。 The answer is that I cannot
add 1;000 to 10。2; because the 1;000 is not significant with respect to the number 10。2。 But I
can add 1;000 to 10;164;818; to get 10;165;818; because the most significant value is a single
whole number。
Relating this back to numeric types; it means integer…based numbers have a most signifi
cant single whole number。 Adding 1。5 and 1。2 as whole numbers results in 3; as illustrated in
Figure 2…15。
Let’s extend this concept of significant digits to a floating…point number type; Single; and
consider the example shown in Figure 2…16。 The {0} construct in the Console。WriteLine() method
is replaced by the second argument。 If there were a third argument; it would be placed in the
resultant string with {1}; a fourth would be replaced with {2}; and so on。
As shown in Figure 2…16; if you want to keep the precision of adding a small number to a
large number; you will need to switch number types to Double。 But even Double has limits and
can remember only 15 to 16 digits of precision。
If you want even more precision; you could use Decimal; but Decimal is more suitable for
financial calculations。 With financial calculations; you will run into the problem of having very
large numbers added to small numbers。 Imagine being Bill Gates and having a few billion in
your bank account。 When the bank calculates the interest; you will want to know how many
pennies you have accumulated; because pennies; when added over a long period of time; make
a big difference。 In fact; some programmers have “stolen” money from banks by collecting the
fractional pennies and accumulating them。
Now that you’ve seen some of the plexities involved when working with numbers; let’s
finish the calculator。
…………………………………………………………Page 68……………………………………………………………
46 CH AP T E R 2 ■ L E A R N IN G AB OU T 。 N E T N U M B E R A N D V A L U E T Y P E S