200 likes | 220 Views
Internal Functions. Integer Functions. Converting to Integers Function Result Explanation x=Int(6.5) 6 Rounds Down x=Int(-6.5) -7 x=Fix(6.5) 6 Truncates at Decimal x=Cint(6.5) 7 Changes to Integer x=Cint(-6.5) -6 x=Round(6.5) 7 .5 rounds up . Some Math Functions.
E N D
Integer Functions Converting to Integers Function Result Explanation x=Int(6.5) 6 Rounds Down x=Int(-6.5) -7 x=Fix(6.5) 6 Truncates at Decimal x=Cint(6.5) 7 Changes to Integer x=Cint(-6.5) -6 x=Round(6.5) 7 .5 rounds up
Some Math Functions Abs(intAge1 - intAge2) Absolute Value Sqr(144) Square Root Exp(10) Natural Log^e Log(10) Natural Log.
Trig Functions • print sin(90 * 3.1415926/180) • sngArc= atn(90 * 3.1415926/180) • x=cos (90 * 3.1415926/180) • y=sin(90 * 3.1415926/180) • myTan=tan(90 * 3.1415926/180) You must convert to radians. Aaaargh. maybe a constant or making a function would be handy • cRads = 3.1415926/180 x=cos(90 * cRads) • y=sin(ToRadians(90))
Data Type Functions • IsDate(x) Can convert to a Date • IsEmpty(x) Never Initialized • IsNull(x) Holds Null Value • IsNumeric(x) Can convert to a Number • VarType(x) Returns number for type of variable
Shortcut Functions • Iif • StrChoice=Iif(intCondition =1, “yes”, “no”) • Choose • StrChoice=Choose(intNumber, “one”, “two”, “three”, “four”)
Data Conversion converts Data From one type to another • blnYesNo = Cbool(intNumber) • Cbyte() • Ccur() • Cdate() • CDbl() • Cdec()
CBool() CByte() CCur() CDate() CDbl() CDec() CInt() CLng() CStr() CVar() Common - CInt and CStr Data Conversion converts Data From one type to another blnYesNo = Cbool(intNumber)
String Functions • Len(“Kevin”) 5 • CStr(1) “1” • Str(1) “ 1” • Str(-1) “-1” • Val(“1”) 1
Ascii Functions • Chr(13) return line • Chr(65) “A” • Useful for multilingual functions and characters not on keyboard. • Asc(“A”) 65
Substring Functions • Left(“Monkey”, 4) “Monk” • Right(“Monkey”, 3) “key” • Mid(“Monkey”, 2, 2) “on”(start at 2, for 2 characters) • Mid can be a command: strVehicle=“boat”Mid(strVehicle, 3,1) = “o”
More String Functions • Ltrim(“ hello”) • Rtrim(“hello “) • Trim (“ hello “) • All return “hello” • Ltrim(“ hello “) returns what? • StrReverse(“Kevin”) returns “neviK”
Dates and Times • Literals must be in the pound signs!#09/26/00# • The computer recognizes a lot of formats. • Date Short for Date(), etc. • Time • Now Date & Time
Dealing with time • Timer returns number of seconds since midnight on your computers clock. • DateAdd(“yyyy”, 10, Now) adds 10 years. You can also add months (“m”) and 8 other intervals to any given date. • DateDiff(“m”, #01/01/80#, Now) • DatePart(“w”, #09/01/00#) produces 6, which stands for Friday. Sunday is 1.
More on Dates • Used to make sure addition of dates is correct. • DateSerial(1988+intYears, 1+intMonths, 1+intDays) • DateSerial(2000, 9, 26+10) = 10/6/00 • DateSerial(2000, 9, 26+100) = 1/4/01 • DateValue(#September 26, 2000#)=9/26/00
TimeSerial() and TimeValue() • Similar to DateSerial() and DateValue()
Formatting • You can format your output. This is particularly useful for numbers. • You can indicate how many decimal places you wish to show, you can indicate phone numbers and zip codes. You can divide up long credit card numbers. You can do much more.
Format(expression, format) • The format argument accepts the following for formatting strings: @ Makes sure a character or blank appears. & An available character appears. ! Forces filling a field from left to right. < Forces characters to lowercase > Forces characters to uppercase : Shows time colon / Shows date slashes There are also 23 date format indicators to indicate what part of the date you want and how you want it.
Format(expression, format) • The format argument accepts the following for formatting numbers: no format No formatting 0 Forces digit to appear # available digit position . decimal position % multiply by 100, add “%” E+, E-, e+, e- Scientific Notation \ Show the next character -, +, $, space Shown as is
Whew! • Reading Chapter 8 is necessary. If you understand the chapter you understand a large portion of Visual Basic. • As you read, experiment in the immediate window. • Don’t feel you have to memorize every function and detail.