Dim D As Double D = 3.16 D = IIf(Abs(D - Int(D)) > 0 And Abs(D - Int(D)) < 1, Int(D) + 1, D) Debug.Print D
代码是不错,不过好像与楼主意思有点出入,我的理解楼主的意思是当小数点后的第一位是0的话,则答案就是小数点前面的整数,如果大于0,则答案就是小数点前面的整数加一.通过我的理解帮你改了一下: Dim d As Double d = 3.06 d = IIf(Abs(d - Int(d)) >= 0.1, Int(d) + 1, Int(d)) Debug.Print d 不知道是否如你的意思.
对1楼2楼的代码改进:Option Explicit Dim s As SinglePrivate Sub Command1_Click() s = Val(Text1) Call zh(s) End SubPublic Function zh(d As Single) As Single d = IIf(Abs(d - Int(d)) >= 0.0999999, Int(d) + 1, Int(d)) Print d End Function
Dim d As Double d = 4.1 d = IIf(Abs(d - Int(d)) >= 0.1, Int(d) + 1, Int(d)) Print d看看是不是等于4呢?
Dim D As Double D = 3.1 D = IIf(Abs(D - Int(D)) >= 0.1, Int(D) + 1, Int(D)) Debug.Print(D)
貌似有个 什么 Round()函数吧?
上面的代码太复杂了,用CLng(x+0.41)即可。
dim pos as integer dim dblNumber as double dim strNumber as string dim intValue as integer dblNumber = 3.05 strNumber = str(dblNumber) pos = instr(strNumber,".") if pos > 0 then intValue = intInt(dblNumber) + 1 else intValue = int(dblNumber) end if
正 CLng(原数+0.5) 负 CLng(原数-0.5)
上面错了Sgn(x) * ((Fix(Abs(x) * 10) + 9) \ 10)
Dim c As Double Dim x As Integer c = 3.16 If c - Int(c) > 0 Then x = Int(c) + 1 Else x = c End If MsgBox x
语法 Int(number) Fix(number)必要的 number 参数是 Double 或任何有效的数值表达式。如果 number 包含 Null,则返回 Null。说明Int 和 Fix 都会删除 number 的小数部份而返回剩下的整数。 Int 和 Fix 的不同之处在于,如果 number 为负数,则 Int 返回小于或等于 number 的第一个负整数,而 Fix 则会返回大于或等于 number 的第一个负整数。例如,Int 将 -8.4 转换成 -9,而 Fix 将 -8.4 转换成 -8。基本思路: 由于你的要求是小数部分如果是>0,就进位,所以最简单的方法是将你的数+0.9,再取整 比如一个数是:5.11,你要得到6,采用 int(5.11+0.9) 5.01,你要得到6,采用 int(5.01+0.9) 如果int()不行,就用Fix()另外提一句:学编程,肯定要有 自己 探索的精神,因为不可能从别人那里拿到一个刚好完全适合你用的代码的。
D = 3.16
D = IIf(Abs(D - Int(D)) > 0 And Abs(D - Int(D)) < 1, Int(D) + 1, D)
Debug.Print D
Dim d As Double
d = 3.06
d = IIf(Abs(d - Int(d)) >= 0.1, Int(d) + 1, Int(d))
Debug.Print d
不知道是否如你的意思.
Dim s As SinglePrivate Sub Command1_Click()
s = Val(Text1)
Call zh(s)
End SubPublic Function zh(d As Single) As Single
d = IIf(Abs(d - Int(d)) >= 0.0999999, Int(d) + 1, Int(d))
Print d
End Function
Dim d As Double
d = 4.1
d = IIf(Abs(d - Int(d)) >= 0.1, Int(d) + 1, Int(d))
Print d看看是不是等于4呢?
Int(5.26 +0.9) = 6
Int(5.06 +0.9) = 5
Dim D As Double
D = 3.1
D = IIf(Abs(D - Int(D)) >= 0.1, Int(D) + 1, Int(D))
Debug.Print(D)
dim dblNumber as double
dim strNumber as string
dim intValue as integer
dblNumber = 3.05
strNumber = str(dblNumber)
pos = instr(strNumber,".")
if pos > 0 then
intValue = intInt(dblNumber) + 1
else
intValue = int(dblNumber)
end if
正
CLng(原数+0.5)
负
CLng(原数-0.5)
Dim x As Integer
c = 3.16
If c - Int(c) > 0 Then
x = Int(c) + 1
Else
x = c
End If
MsgBox x
Int(number)
Fix(number)必要的 number 参数是 Double 或任何有效的数值表达式。如果 number 包含 Null,则返回 Null。说明Int 和 Fix 都会删除 number 的小数部份而返回剩下的整数。
Int 和 Fix 的不同之处在于,如果 number 为负数,则 Int 返回小于或等于 number 的第一个负整数,而 Fix 则会返回大于或等于 number 的第一个负整数。例如,Int 将 -8.4 转换成 -9,而 Fix 将 -8.4 转换成 -8。基本思路:
由于你的要求是小数部分如果是>0,就进位,所以最简单的方法是将你的数+0.9,再取整
比如一个数是:5.11,你要得到6,采用 int(5.11+0.9)
5.01,你要得到6,采用 int(5.01+0.9)
如果int()不行,就用Fix()另外提一句:学编程,肯定要有 自己 探索的精神,因为不可能从别人那里拿到一个刚好完全适合你用的代码的。