chapter 8. visual basic includes several built-in mathematical functions ◦ abs ◦ sqr ◦ sgn...

16
Chapter 8

Upload: malcolm-morrison

Post on 21-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 8.  Visual Basic includes several built-in mathematical functions ◦ Abs ◦ Sqr ◦ Sgn ◦ IsNumeric ◦ Round ◦ Format ◦ Pmt ◦ PV ◦ FV

Chapter 8

Page 2: Chapter 8.  Visual Basic includes several built-in mathematical functions ◦ Abs ◦ Sqr ◦ Sgn ◦ IsNumeric ◦ Round ◦ Format ◦ Pmt ◦ PV ◦ FV

Visual Basic includes several built-in mathematical functions◦ Abs◦ Sqr◦ Sgn◦ IsNumeric◦ Round◦ Format◦ Pmt◦ PV◦ FV

Page 3: Chapter 8.  Visual Basic includes several built-in mathematical functions ◦ Abs ◦ Sqr ◦ Sgn ◦ IsNumeric ◦ Round ◦ Format ◦ Pmt ◦ PV ◦ FV

Returns the absolute value of a number Form: Abs(number) Example: Dim intNum As IntegerintNum = -5lblAnswer.Caption = Abs(intNum)‘ 5 will be displayed

Page 4: Chapter 8.  Visual Basic includes several built-in mathematical functions ◦ Abs ◦ Sqr ◦ Sgn ◦ IsNumeric ◦ Round ◦ Format ◦ Pmt ◦ PV ◦ FV

Returns the square root of a number You can not take a square root of a negative number Form: Sqr(number) Example:Dim intNum As IntegerintNum = 9lblAns.Caption = Sqr(intNum)‘ 3 will be displayed

Page 5: Chapter 8.  Visual Basic includes several built-in mathematical functions ◦ Abs ◦ Sqr ◦ Sgn ◦ IsNumeric ◦ Round ◦ Format ◦ Pmt ◦ PV ◦ FV

Returns 1,-1,or 0 when the numeric argument is positive, negative or 0 respectively.

Form: Sgn(number) Example:Dim intNum As IntegerintNum = -5lblAns.Caption = Sgn(intNum) ‘-1 is displayedintNum = 3 lblAns.Caption = Sgn(intNum) ‘1 is displayed

Page 6: Chapter 8.  Visual Basic includes several built-in mathematical functions ◦ Abs ◦ Sqr ◦ Sgn ◦ IsNumeric ◦ Round ◦ Format ◦ Pmt ◦ PV ◦ FV

Returns True if its argument can be evaluated as a number and False if it cannot.

Form: IsNumeric(argument) Example: Dim strString As StringstrString = “123”lblAns.Caption = IsNumeric(strString) ‘TruestrString = “abc”lblAns.Caption = IsNumeric(strString) ‘FalselblAns.Caption = IsNumeric(“2+4”) ‘FalselblAns.Caption = IsNumeric(2+4) ‘ True

Page 7: Chapter 8.  Visual Basic includes several built-in mathematical functions ◦ Abs ◦ Sqr ◦ Sgn ◦ IsNumeric ◦ Round ◦ Format ◦ Pmt ◦ PV ◦ FV

Rounds numeric data to specified number of decimal places.

Form: Round(number, decimal places) Example:Dim dblNum As DoubledblNum = 3.4567lblAns.Caption = Round(dblNum, 2)‘3.46 will be displayed

Page 8: Chapter 8.  Visual Basic includes several built-in mathematical functions ◦ Abs ◦ Sqr ◦ Sgn ◦ IsNumeric ◦ Round ◦ Format ◦ Pmt ◦ PV ◦ FV

Used to format, or change the appearance of, numeric data.

Form: Format(number, “format type”) Examples:lblAns.Caption = Format(8789, “General Number”) ‘8789lblAns.Caption = Format(8789, “Currency”) ‘$8,789.00lblAns.Caption = Format(8789, “Fixed”) ‘8789.00lblAns.Caption = Format(8789, “Standard”) ‘8,789.00lblAns.Caption = Format(89, “Percent”) ‘8900.00%lblAns.Caption = Format(1, “Yes/No”) ‘YeslblAns.Caption = Format(0, “True/False”) ‘FalselblAns.Caption = Format(1, “On/Off”) ‘On

Page 9: Chapter 8.  Visual Basic includes several built-in mathematical functions ◦ Abs ◦ Sqr ◦ Sgn ◦ IsNumeric ◦ Round ◦ Format ◦ Pmt ◦ PV ◦ FV

Three business Built-in function• Pmt(rate, term, principal)• PV(rate, term, payment)• FV(rate, term, investment)

Page 10: Chapter 8.  Visual Basic includes several built-in mathematical functions ◦ Abs ◦ Sqr ◦ Sgn ◦ IsNumeric ◦ Round ◦ Format ◦ Pmt ◦ PV ◦ FV

Returns the periodic payment for an installment loan. Rate- is the interest rate per periodTerm – is the total number of payments to be madePrincipal – is the amount borrowed

Example: Calculate the monthly payments on a 30 – year, $100,000 loan with an annual interest rate of 6%.

dblPay = Pmt(0.06/12, 360, -100000)lblAns.Caption = Format(dblPay, “Currency”) ‘$599.55***Since payments are monthly, the interest rate must also be

computed monthly by dividing the annual rate interest 6% by 12.

***The principal is negative because it is the amount borrowed.

Page 11: Chapter 8.  Visual Basic includes several built-in mathematical functions ◦ Abs ◦ Sqr ◦ Sgn ◦ IsNumeric ◦ Round ◦ Format ◦ Pmt ◦ PV ◦ FV

Returns the present value of an investment.Rate- is the interest rate per periodTerm – is the total number of payments to be madePayment – amount invested per periodExample: Determine the cost of financing a car. $250 per

month over a 4 year period is applied to a loan with an interest rate of 8%.

dblAmount = 250 * 12 * 4 ‘12000dblPresent = PV(0.08/12, 48, -250) ‘10240.48dblFin = dblAmount – dblPresentlblAns.Caption = Format(dblFin, “Currency”) ‘$1,759.52***This means that $1,759.52 is spent on Financing

Page 12: Chapter 8.  Visual Basic includes several built-in mathematical functions ◦ Abs ◦ Sqr ◦ Sgn ◦ IsNumeric ◦ Round ◦ Format ◦ Pmt ◦ PV ◦ FV

Returns what a series of equal payments invested at a fixed interest will be worth after a period of time.Rate- is the interest rate per periodTerm – is the total number of payments to be madeInvestment – is the amount invested per period Example: You invest $500 per month in a retirement

plan earning 8% interest per year. What would it be worth in 20 years.

dblFuture = FV(0.08/12, 240, -500)lblAns.Caption = Format(dblFuture, “ Currency”)

‘$294,510.21

Page 13: Chapter 8.  Visual Basic includes several built-in mathematical functions ◦ Abs ◦ Sqr ◦ Sgn ◦ IsNumeric ◦ Round ◦ Format ◦ Pmt ◦ PV ◦ FV

Allows the user to select a value from a set of values.

Use the Icon to create a List BoxProperties of a List Box• Name – use the prefix “lst” for the name• List – stores a group of strings that makes up the list.• Sorted – displays the item in the list in alphabetical

order. (Design Time only)• Text – stores the selected list item. (Run Time Only)• ListCount – can be used at run time to determine

number of items in the list.

Page 14: Chapter 8.  Visual Basic includes several built-in mathematical functions ◦ Abs ◦ Sqr ◦ Sgn ◦ IsNumeric ◦ Round ◦ Format ◦ Pmt ◦ PV ◦ FV

Click Event – is executed when the user clicks on an item in the list box.

AddItem Method – is used to add an item to the list◦ Example: lstControl.AddItem Item◦ lstControl – is the name of the list and item is a string

object or string double quotes added to the list box Clear – delete List Box contents of the list.

◦ Example: lstControl.Clear

Page 15: Chapter 8.  Visual Basic includes several built-in mathematical functions ◦ Abs ◦ Sqr ◦ Sgn ◦ IsNumeric ◦ Round ◦ Format ◦ Pmt ◦ PV ◦ FV

Also called collapsible list The icon is used to create it. Properties:• Name – use the prefix “cbo” for the name• List – stores a group of strings that makes up the items in the

combo box.• Sorted – displays the item in the combo box in alphabetical order.

(Design Time only)• Text – stores the selected item in the combo box. (Run Time Only)• ListCount – can be used at run time to determine number of items

in the combo box• Style – dropdown combo, simple combo, or dropdown list. Runtime

use vbComboDropDown, vbComboSimple, or vbComboDrop-DownList.

Page 16: Chapter 8.  Visual Basic includes several built-in mathematical functions ◦ Abs ◦ Sqr ◦ Sgn ◦ IsNumeric ◦ Round ◦ Format ◦ Pmt ◦ PV ◦ FV

Click Event – is executed when the user clicks on an item in the combo box.

AddItem Method – is used to add an item to the list◦ Example: cboControl.AddItem Item◦ cboControl – is the name of the list and item is a

string object or string double quotes added to the list

Clear – delete Combo Box contents of the list Example: cboControl.Clear