新浪博客

ASP中Cint(),int(),fix(),round()几个函数的区别和用法

2012-06-15 02:36阅读:
CInt 会进行四舍五入取最接近它的偶数。
如 Cint(0.5)=0 ; Cint(1.5)=2 ; Cint(2.5)=2 ;Cint(3.5)=4
而Int就是取整函数,会去除小数点部分.如 Int(1.5)=1 ...
这样的定位不准确,按测试的角度是漏测. 第一个:Cint函数没有考虑到Cint(0.51)=1,Cint(2.51)=3的情况.第二个:Int函数没有考虑到负数的情况:Int(-1.1)=-2,Int(-1.5)=-2的情况 所以我总结如下: int函数:数轴上往左取整。 Cint函数:四舍五入取整,无论正数还是负数。但是*.5或-*.5时,则往取偶数值。 但注意:Cint(0.51)=1,Cint(-0.51)也同样等于-1.
总结:
int函数:数轴上往左取整。
Cint函数:四舍五入取整,无论正数还是负数。但是*.5或-*.5时,则往取偶数值。
Cint(0.5)=0 -> 0
Cint(0.51)=1 -> 1
Cint(1.5)=2 -> 2
Cint(2.5)=2 -> 2
Cint(2.51)=3 -> 3
Cint(2.6)=3 -> 3
Cint(2.62)=3 -> 3
Cint(3.5)=4 -> 4
Cint(3.3)=3 -> 3
Cint(1.2)=1 -> 1
Int(-1.1)=-2 -> -2
Int(-1.5)=-2 -> -2

asp中的几个取整函数是:fix(),int(),round();
Int(number)、Fix(number)函数返回数字的整数部分。number 参数可以是任意有效的数值表达式。如果 number 参数包含 Null
,则返回 Null。
例:
response.write int(2.14) '2
response.write fix(2.14) '2
response.write int(2.54) '2
response.write int(2.54) '2
Int 和 Fix 函数都删除 number 参数的小数部分并返回以整数表示的结果。Int 和 Fix 函数的区别在于如果 number 参数为负数时,Int 函数返回小于或等于 number 的第一个负整数,而 Fix 函数返回大于或等于 number 参数的第一个负整数。例如,Int 将 -8.4 转换为 -9,而 Fix 函数将 -8.4 转换为 -8。
round(Expression[, numdecimalplaces])返回按指定位数进行四舍五入的数值。Expression是必选项。数值表达式 被四舍五入。Numdecimalplaces是可选项。数字表明小数点右边有多少位进行四舍五入。如果省略,则 Round 函数返回整数。
例:
response.write round(3.14) '3
response.write round(3.55) '4
response.write round(3.1415,3) ' 3.142

我的更多文章

下载客户端阅读体验更佳

APP专享