《VB2008从入门到精通(PDF格式英文版)》第28章


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 large numbers like 2 billion; but has the
problem that you cannot add fractional numbers like 1。5 plus 1。5。 
o Double: Solves the problem of adding two very large or small numbers; and can be used
to add fractional numbers。 Generally speaking; Double is a good choice; but can suffer
from significance problems if a very large number is manipulated by a very small number。 
o Decimal: A generally good approach and suitable for all types of precision; but also the
slowest when adding; subtracting; or performing other mathematical operations。 
The simplest all…around numeric data type to use is Double; as it provides good precision
and is relatively fast。 The plete implementation of the calculator is as follows: 
Public Class Operations 
Public Shared Function Add(ByVal number1 As Double; ByVal number2 As _ 
Double) As Double 
Return number1 + number2 
End Function 
Public Shared Function Divide(ByVal number1 As Double; ByVal number2 As _ 
Double) As Double 
Return number1 / number2 
End Function 
Public Shared Function Multiply(ByVal number1 As Double; ByVal number2 As _ 
Double) As Double 
Return number1 * number2 
End Function 
Public Shared Function Subtract(ByVal number1 As Double; ByVal number2 As _ 
Double) As Double 
Return number1 number2 
End Function 
End Class 
The four operations are methods with different identifiers; but identical method signa
tures; making it easy to remember how to use each method。 Each of the operations would have
an appropriate set of tests verifying the correctness of the implementation。 The tests are not
reproduced here; but they are implemented in the sample source code。 I advise you to take a
quick look at the tests to make sure you understand the individual pieces。 
…………………………………………………………Page 70……………………………………………………………
48 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
The Important Stuff to Remember 
In this chapter; you learned about developing a class library that is used to perform some
calculations。 The following are the key points to remember: 
o Organization of your thoughts; projects; and features makes all the difference when
writing software。 
o When writing software; stay focused。 It is very easy to drift around in software development;
because software lets you stray easily。 A successful developer will always be organized
and focused。 
o Software is designed using an architecture that could be implemented top…down or
bottom…up。 
o Within an architecture; individual pieces are called ponents; and they fit together to
create
小说推荐
返回首页返回目录