将变量四舍五入为整数
Rounding a variable to an integer
我有这样的计算:3 * 12300 / 160。结果是:230.625。但我只想要整数部分,230.
在 C 中,这可以使用如下方式完成:int MyVar = (int)3*12300/160;
VBA(使用 MS-Access)是否有办法强制结果为整数?
您可以使用 Int
or Fix
functions 向下取整。
既然你知道你想要的结果是一个整数,你应该将结果存储在一个类型为 Long
的变量中(或者 Integer
如果你绝对确定它总是更小比 32768)。
Dim l As Long
l = Int(3 / 160 * 12300) ' <~~~~ Yes, I switched the numbers around on purpose!*
MsgBox "l = " & l
* 为什么我调换了数字?因为表达式 3 * 12300 / 160
会在 VBA 中抛出错误。在这里阅读原因:Overflow when multiplying Integers and assigning to Long
我有这样的计算:3 * 12300 / 160。结果是:230.625。但我只想要整数部分,230.
在 C 中,这可以使用如下方式完成:int MyVar = (int)3*12300/160;
VBA(使用 MS-Access)是否有办法强制结果为整数?
您可以使用 Int
or Fix
functions 向下取整。
既然你知道你想要的结果是一个整数,你应该将结果存储在一个类型为 Long
的变量中(或者 Integer
如果你绝对确定它总是更小比 32768)。
Dim l As Long
l = Int(3 / 160 * 12300) ' <~~~~ Yes, I switched the numbers around on purpose!*
MsgBox "l = " & l
* 为什么我调换了数字?因为表达式 3 * 12300 / 160
会在 VBA 中抛出错误。在这里阅读原因:Overflow when multiplying Integers and assigning to Long