嗔秤戻幣�哉膵�云利匈嬉蝕湊蛸賜�塋床四衲���萩晦編報炎嘔囚^泡仟 ̄云利匈�《超噌�殻窟�嵌虜隆輓麈觚翹瀘卉韮�仍仍�。� 烏御危列
浪慕利 卦指云慕朕村 厘議慕尺 厘議慕禰 TXT畠云和墮 序秘慕杏 紗秘慕禰

VB2008貫秘壇欺娼宥(PDF鯉塀哂猟井)-及21嫗

梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��




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 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
卦指朕村 貧匯匈 和匯匈 指欺競何 壘��0�� 家��0��
隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
梁椣戻幣�� 梁心弌傍議揖扮窟燕得胎��傍竃徭失議心隈才凪万弌誌育断蛍�輌臆惨軼僑〃�燕慕得珊辛參資誼持蛍才将刮襲潜��範寔亟圻幹慕得 瓜寡追葎娼得辛參資誼寄楚署衛、持蛍才将刮襲潜填��