友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!
VB2008从入门到精通(PDF格式英文版)-第15部分
快捷操作: 按键盘上方向键 ← 或 → 可快速上下翻页 按键盘上的 Enter 键可回到本书目录页 按键盘上方向键 ↑ 可回到本页顶部! 如果本书没有阅读完,想下次继续接着阅读,可使用上方 "收藏到我的浏览器" 功能 和 "加入书签" 功能!
late the largest possible number a data type can store; you use 2 to the power of the number of
spaces and then subtract 1。 In the case of the Integer type; there are 32 spaces。 Before we calculate
the biggest number Integer can store; though; we need to consider negative numbers。 The
upper limit of Integer isn’t actually 4;294;967;295 (the result of 232 – 1); because Integer also
stores negative numbers。 In other words; it can save a negative whole number; such as –2。
The puter uses a trick in that the first space of the number is reserved for the sign (plus
or minus) of the number。 In the case of Integer; that means there are only 31 spaces for
numbers; so the largest number that can be represented is 2;147;483;647; and the smallest is
–2;147;483;648。 Going back to our addition example; this fact means that when the result of our
addition is 4 billion; which in binary requires 32 spaces; Integer does not have the space to store it。
The environment includes the numeric data types listed in Table 2…1; which have
varying sizes and storage capabilities。 The following terminology is used to describe numeric
data types:
o A bit is a space of storage; and 8 bits make a byte。
o Integers are whole numbers。
o Floating…point types are fractional numbers。
o Signed means one space in the number is reserved for the plus or negative sign。
Table 2…1。 Numeric Data Types
Type Description
Byte Unsigned 8…bit integer; the smallest value is 0; and the largest value is 255
SByte Signed 8…bit integer; the smallest value is –128; and the largest value is 127
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
Fractional number one and a half
Public Sub AddFractionalNumbersToWhole()
Dim total As Integer = CType(1。5; Integer) + _
CType(1。2; Integer)
If (total 2。7) Then
Console。WriteLine(〃Oops; something went wrong〃)
End If
End Sub
Using Ctype() means to cast a Double
to an Integer。 This results in a
roundoff where 1。2 = 1 and 1。5 = 2。
Had DirectCast() been used; a
pilation error would result; since
converting a Double to an Integer
requires coercion。
Figure 2…15。 Adding fractions using the Integer data type
Declaring a number with a decimal
A really small number
(。0) automatically creates a floating
point value
Public Sub AddFractionalNumbers()
Dim value As Single = 10000。0 + 0。000001
Console。WriteLine(〃Value ({0})〃; value)
End Sub
The console displays the number
The variable value is 10;000 because the small number is
the addition of a big insignificant from the perspective of
number to a small Single。 Single can remember only 7
number digits。 10000。000001 is 10 digits of
precision。
Figure 2…16。 Adding fractions using the Single data type
…………………………………………………………Page 69……………………………………………………………
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 47
Finishing the Calculator
The original declaration of the Add() method for the calculator worked; but had some serious
limitations on what kinds of numbers could be added。 To finish the calculator; we need to
declare the Add() method using a different type; and then add the remaining operations。
We could use one of three types to declare the Add() method:
o Long: Solves the problem of adding two very
快捷操作: 按键盘上方向键 ← 或 → 可快速上下翻页 按键盘上的 Enter 键可回到本书目录页 按键盘上方向键 ↑ 可回到本页顶部!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!