VBScript:VBScript Numbers

Numbers可帮助开发人员高效地处理数字,并帮助他们转换其子类型。它还可以帮助他们利用与VBScript关联的内置数学函数。

号码转换功能

数字功能可帮助我们将给定的数字从一种数据子类型转换为另一种数据子类型。

Syntax

Variable_name = Conversion_function_name(expression)
序号功能说明
1CDbl

一个函数,它将给定数量的任何变体子类型转换为double

2CInt

一个函数,它将给定数量的任何变体子类型转换为Integer

3CLng

一个将给定数量的任何变体子类型转换为Long的函数

4CSng

一个函数,它将给定数量的任何变体子类型转换为Single

5Hex

一个函数,它将给定数量的任何变体子类型转换为十六进制

Example

尝试下面的示例了解VBScript中可用的所有数字转换功能。

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         x = 123
         y = 123.882
         document.write("x value after converting to double - " & CDbl(x) & "<br />")
         
         document.write("y value after converting to double - " & CDbl(y) & "<br />")
         
         document.write("x value after converting to Int -" & CInt(x) & "<br />")
         
         document.write("y value after converting to Int -" & CInt(y) & "<br />")
         
         document.write("x value after converting to Long -" & CLng(x) & "<br />")
         
         document.write("y value after converting to Long -" & CLng(y) & "<br />") 
         
         document.write("x value after converting to Single -" & CSng(x) & "<br />")
         
         document.write("y value after converting to Single -" & CSng(y) & "<br />") 
         
         document.write("x value after converting to Hex -" & Hex(x) & "<br />")
         
         document.write("y value after converting to Hex -" & Hex(y) & "<br />") 
      </script>
   </body>
</html>

执行后,上面的脚本将产生以下输出:

x value after converting to double - 123
y value after converting to double - 123.882
x value after converting to Int -123
y value after converting to Int -124
x value after converting to Long -123
y value after converting to Long -124
x value after converting to Single -123
y value after converting to Single -123.882
x value after converting to Hex -7B
y value after converting to Hex -7C

Number格式化

Number格式化功能可帮助开发人员以他们希望的格式表示给定的数字。

语法

variablename = Format_function_Name(Expression[,NumberDigAfterDec[,LeadingDig[,
UseParForNegNum[,GroupDigits]]]]) 

描述

  • 必需参数Format_function_Name对应于下面列出的任何数字格式化功能。
  • 可选参数Expression对应于任何数字表达式,这将导致一个数字。
  • 可选参数NumberDigAfterDec对应于小数点后的位数。
  • 可选参数LeadingDig对应于分数值是否显示前导零。它基于以下设置参数采用三个值之一。
  • 可选参数UseParForNegNum对应于是否在括号内放置负值。它基于以下设置参数采用三个值之一。
  • 可选参数GroupDigits对应于是否使用组定界符对数字进行分组。它基于以下设置参数采用三个值之一。

设定值

上面的参数LeadingDig,UseParForNegNum和GroupDigits参数可以具有以下任何设置-

  • -2 = vbUseDefault-使用计算机的区域设置
  • -1 = vbTrue-真
  • 0 = vbFalse-错误

请尝试下面的示例,以了解VBScript中所有可用的数字格式功能。

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">

         Dim num : num = -645.998651

         document.write("Line 1 : " & FormatNumber(num, 3))& "<br/>"

         ' The UseParensForNegativeNumbers parameter is set to true.
         document.write("Line 2 : " & FormatNumber (num, 3, , vbTrue))&" <br/> "

         ' The GroupDigits parameter is set to false.
         document.write("Line 3 : " & FormatNumber (num, 3, , , vbFalse)) & "<br/>"

         document.write("Line 4 : " & FormatPercent(num, 3))& "<br/>"

         ' The UseParensForNegativeNumbers parameter is set to true.
         document.write("Line 5 : " & FormatPercent (num, 3, , vbTrue))&" <br/> "

         ' The GroupDigits parameter is set to false.
         document.write("Line 6 : " & FormatPercent (num, 3, , , vbFalse)) & "<br/>"

      </script>
   </body>
</html>

当执行上述脚本时,以下是输出:

Line 1 : -645.999
Line 2 : (645.999) 
Line 3 : -645.999
Line 4 : -64,599.865%
Line 5 : (64,599.865%) 
Line 6 : -64599.865%
序号功能说明
1FormatNumber

一个函数,它将返回一个格式化为数字的表达式

2FormatPercent

一个函数,它将返回一个格式化为百分比的表达式

数学(Mathematical)函数

数学(Mathematical)函数可帮助我们评估给定输入数字的数学和三角函数。

语法

variablename = Mathematical_function_Name(Expression) 

数学函数可帮助我们评估给定输入数字的数学和三角函数。请尝试以下示例,以了解VBScript中可用的所有内置数学函数。

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">

         Dim num1 : num1 = -645.998651
         Dim num2 : num2 = 210

         document.write("int Result of num1 is : " & int(num1))& "<br/>"
         document.write("int Result of num2 is : " & int(num2))& "<br/>"

         document.write("Fix Result of num1 is : " & Fix(num1))& "<br/>"
         document.write("Fix Result of num2 is : " & Fix(num2))& "<br/>"

         document.write("Log Result of num2 is : " & Log(num2))& "<br/>"

         document.write("Oct Result of num1 is : " & Oct(num1))& "<br/>"
         document.write("Oct Result of num2 is : " & Oct(num2))& "<br/>"

         document.write("Hex Result of num1 is : " & Hex(num1))& "<br/>"
         document.write("Hex Result of num2 is : " & Hex(num2))& "<br/>"

         document.write("Rnd Result of num1 is : " & Rnd(num1))& "<br/>"
         document.write("Rnd Result of num2 is : " & Rnd(num2))& "<br/>"

         document.write("Sgn Result of num1 is : " & Sgn(num1))& "<br/>"
         document.write("Sgn Result of num2 is : " & Sgn(num2))& "<br/>"

         document.write("Sqr Result of num2 is : " & Sqr(num2))& "<br/>"

         document.write("Abs Result of num1 is : " & Abs(num1))& "<br/>"
         document.write("Abs Result of num2 is : " & Abs(num2))& "<br/>"

         document.write("Exp Result of num1 is : " & Exp(num1))& "<br/>"
         document.write("Exp Result of num2 is : " & Exp(num2))& "<br/>"

         document.write("Sin Result of num1 is : " & Sin(num1))& "<br/>"
         document.write("Sin Result of num2 is : " & Sin(num2))& "<br/>"

         document.write("Cos Result of num1 is : " & Cos(num1))& "<br/>"
         document.write("Cos Result of num2 is : " & Cos(num2))& "<br/>"

         document.write("Tan Result of num1 is : " & Tan(num1))& "<br/>"
         document.write("Tan Result of num2 is : " & Tan(num2))& "<br/>"
      </script>
   </body>
</html>

当执行上述脚本时,以下是输出:

int Result of num1 is : -646
int Result of num2 is : 210
Fix Result of num1 is : -645
Fix Result of num2 is : 210
Log Result of num2 is : 5.34710753071747
Oct Result of num1 is : 37777776572
Oct Result of num2 is : 322
Hex Result of num1 is : FFFFFD7A
Hex Result of num2 is : D2
Rnd Result of num1 is : 0.5130115
Rnd Result of num2 is : 0.5615935
Sgn Result of num1 is : -1
Sgn Result of num2 is : 1
Sqr Result of num2 is : 14.4913767461894
Abs Result of num1 is : 645.998651
Abs Result of num2 is : 210
Exp Result of num1 is : 2.79479883633128E-281
Exp Result of num2 is : 1.59162664037792E+91
Sin Result of num1 is : 0.920530264916375
Sin Result of num2 is : 0.467718518342759
Cos Result of num1 is : 0.390671257418547
Cos Result of num2 is : -0.883877473182372
Tan Result of num1 is : 2.35627845006822
Tan Result of num2 is : -0.529166691689464
序号功能说明
1Int

一个函数,它返回给定数字的整数部分

2Fix

一个函数,它返回给定数字的整数部分

3Log

一个函数,它返回给定数字的自然对数。不允许负数

4Oct

一个函数,该函数返回给定百分比的八进制值

5Hex

一个函数,该函数返回给定数字的十六进制值

6Rnd

一个函数,返回0到1之间的随机数

7Sgn

一个函数,它返回一个与指定数字的符号相对应的数字

8Sqr

一个函数,它返回给定数字的平方根。不允许负数

9Abs

一个函数,它返回给定数字的绝对值

10Exp

一个函数,该函数将e的值返回指定的数字

11Sin

一个函数,返回给定数字的正弦值

12Cos

一个函数,返回给定数字的余弦值

13Tan

一个函数,该函数返回给定数字的正切值

作者:terry,如若转载,请注明出处:https://www.web176.com/vbscript/1245.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2020年12月8日 下午5:35
下一篇 2020年12月9日 下午5:16

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注