2016硕果团队风采:怎样用vb四舍五入求近似值?

来源:百度文库 编辑:高考问答 时间:2024/04/30 19:44:07
比如:3.1415926
我想让它精确到哪一位叫到哪一位!

使用round函数:返回一个数值,该数值是按照指定的小数位数进行四舍五入运算的结果。
例如:round(3.25,1)=3.3
  round(4.225,2)=4.23
  round(5.395,2)=5.40

VB中有专门用来四舍五入的函数

Round 函数:返回按指定位数进行四舍五入的数值。
语法:Round(expression[, numdecimalplaces])
参数:expression(必选项)-被四舍五入的数值表达式 ;numdecimalplaces(可选项)-数字表明小数点右边有多少位进行四舍五入,如果省略则 Round 函数返回整数。

下面的示例利用 Round 函数将数值四舍五入到两位小数:
Dim MyVar, pi
pi = 3.14159
MyVar = Round(pi, 2) 'MyVar contains 3.14。

Print Format(3.1415926, "0.000000")'精确到六位
Print Format(3.1415926, "0.00000")'五位
Print Format(3.1415926, "0.0000")'四位
Print Format(3.1415926, "0.000")'三位
.........
........
如此类推

x精确到小数点后n位:
int(x*10^n+0.5)/10^n