Commonly used VB functions

math function
function illustrate example
Without(N) Returns the sine of the argument N Sin(0)=0 N is radians
Cos(N) Returns the cosine of the argument N Cos(0)=1 N is radians
So(N) Returns the tangent of the argument N Tan(0)=0 N is radians
Atn(N) Returns the arctangent of the argument N Atn(0)=0 function value is radian
Sgn(N) Return the sign of the argument N (N<0, return -1
N=0, return 0
N>0, return 1)
Sgn(-35)=-1
Sgn(0)=0
Sgn(35)=1
Abs(N) Returns the absolute value of the argument N Abs(-3.5)=3.5
Sqr(N) Returns the square root of the argument N, N≧0 Sqr(9)=3
Exp(N) Return the Nth power value of e, N≧0 Exp(3)=20.0855369231877
Log(N) Returns the natural logarithm of N, N>0 Log(10)=2.3
Int(N) Returns the largest integer not greater than N Int(3.6)=3
Int(-3.6)=-4
Fix(N) returns the integer part of N Fix(-3.3)=-3
Fix(3.6)=3
Belt(s) Returns the rounded integer of N Cint(3.6)=4
Rnd[(N)] Returns a random decimal number between 0 and 1
Round(N1,N2) Round N1 by N2 decimal places. If N2 is omitted, N1 will return an integer Round(4.844)=5
Round(5.7383,3)=5.738
Note: Distinguish the similarities and differences of partition integer Int(), Fix() and Round().


 

 

conversion function
function illustrate example
Asc(X$) Returns the ASCII code value of the first character of X$ Asc("abc")=97
Chr$(X) Convert the value of X to the corresponding ASCII character Chr(97)="a"
Hex$(X) Convert the decimal number X to hexadecimal, which is a numeric string Hex(65535)=5
Oct$(X) Convert the decimal number X to octal, which is a numeric string Oct(65535)=177777
Str$(X) Convert the value of X to a string Str(100)="100"
UCase$(X) Convert lowercase letters in X values ​​to uppercase letters UCase("aBcDefg")="ABCDEFG"
LCase$(X) Convert uppercase letters in X values ​​to lowercase letters LCase("aBcDefg")="abcdefg"
Val(X) Converts the numeric string X to a numeric value Val("123")=123
IsNumeric(X$) Return True if X$ is a numeric string  IsNumeric("abc")=False
CInt(X) Round the decimal part of X and convert it to an integer CInt(-3.64)=-4
CCur(X) Convert the value of X to a currency type value, with up to 4 decimal places and automatic rounding CCur(3.1236568)=3.1237
The converted 3.1237 is the currency type
CDbl(X) Convert the X value to a double precision number CDbl(1.2345678!)=1.2345677614212#
CLng(X) Round off the decimal part of X and convert it to a long integer CL of(1234.5678)=1235
CSng(X) Convert the X value to a single precision number CSng(1.2345677614212)=1.234568
CVar(X) Converts an X value to a Variant type value a$=CVar(123)
b$="abc"+a$
?b$
"abc123"
注意:Str()函数将非负数值转换成字符类型,会在转换后的字符串左边增加空格,即数值的符号位。例如表达式:"abc"+Str(123)的结果为"abc 123"而不是"abc123"。
      Val()将数字字符串转换为数值类型,当字符串中出现数值类型规定的字符外的字符,则停止转换,函数返回的是停止转换前的结果。例如表达式:Val("-123.45ty")的结果为-      123.45;而表达式Val(-123.45E3)结果为123450,因为此时E是指数符号。

 

字符串函数
函数 说明 示例
LTrim$(C$) 去掉C$左边的空白字符 LTrim$("  abc")="abc"
RTrim$(C$) 去掉C$右边的空白字符 RTrim$("abc  ")="abc"
Trim$(C$) 去掉C$两边的空白字符 Trim$("  abc  ")="abc"
Left$(C$,n) 取C$字符串左部的n个字符 Left$("abc",2)="ab"
Right$(C$,n) 取C$字符串右部的n个字符 Right$("abc",2)="bc"
Mid$(C$,p,n) 从位置p开始取C$字符串的n个字符 Mid$("abcdefg",3,3)="cde"
Len(C$) 返回C$字符串的长度 Len("VB 程序设计")=6
LenB(C$) 返回C$字符串的字节数 LenB("VB 程序设计")=12
LenB(StrConv("中国人123", vbFromUnicode))=9
String$(n,C$) 返回由C$首字符组成的字符串,字符串长度为n String$(2,"abc")="aa"
Space$(n) 返回n个空格 Space$(3)="   "
InStr([n,]C1$,C2$[,M])
InStrRev
在C1中从n开始找C2,查到返回位置,否则返回0。省略n表示从头开始找,M表示是否区分大小写。 InStr(2,"abce","bc")=2

InStr(2,"abce","d")=0
Join(A[,D]) 将数值A各元素按D(或空格)分隔符连接成字符串变量 A=array("123","BC","c")

Join(A,"")=123BCc
Replace$(C$,C1$,
C2$[,N1][,N2][,M])
在C字符串中从1(或N1)开始将C2替代C1(有N2,替代N2次) Replace$("abcdabcd","cd","123")
="ab123ab123"

 

日期函数
函数 说明 示例Now 
Now  返回系统当前的日期和时间 Now=2021/9/6 星期一 19:23:03
Date 返回低当前的日期 Date=2021/9/6 星期一
Time 返回系统当前的时间 Time=17:20:18
Day(Now) 返回当前的日(1-31) Day(Now)=8
WeeDay(Now) 返回当前星期的第几天 WeekDay(Now)=7
Month(Now) 返回当前的月份(1-12) Month(Now)=6
Year(Now) 返回当前的年份 Year(Now)=2022
Hour(Now) 返回小时(0-23) Hour(Now)=17
Minute(Now) 返回分钟(0-59) Minute(Now)=20
Second(Now) 返回秒(0-59) Second(Now)=1

 

 

おすすめ

転載: blog.csdn.net/ty5858/article/details/129693916