为什么 Excel VBA 四舍五入我的整数除法结果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16814018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is Excel VBA rounding my integer division results?
提问by Nigel Stevens
I'm writing a VBA macro in Excel 2002 to manipulate date from a device that gives 10 values on each of 4 channels.
我正在 Excel 2002 中编写一个 VBA 宏来操作来自设备的日期,该设备在 4 个通道中的每一个通道上提供 10 个值。
When I try to divide an integer by another integer to calculate the row and column, the result shows that it's using bankers rounding, as if it was a floating point variable.
当我尝试将一个整数除以另一个整数来计算行和列时,结果显示它使用的是银行家舍入,就好像它是一个浮点变量。
Sub TestRounding()
Dim Y As Integer
Dim Field As Integer
Dim Channel As Integer
ActiveWorkbook.Sheets.Add
ActiveWorkbook.ActiveSheet.Cells.Select
range("A1").Select
For Field = 1 To 40
Channel = (Field - 1) / 10
Y = Field - 1
ActiveSheet.range("A1").Offset(Y, Channel).Value = Field
Next Field
End Sub
Y is the row to put the output in, which I usually set with
Y 是放置输出的行,我通常设置它
Y = (Field -1) mod 10
but I've left it as
但我已经把它作为
Y = Field - 1
to illustrate the point more clearly in the resulting worksheet.
在生成的工作表中更清楚地说明这一点。
Channel is supposed to be the column that the output is put in.
Channel 应该是放置输出的列。
When I run this macro, the values 1-6 get put in column A, then 7-15 get put into Column B, 16-26 get put into Column C, then 27-35 in Column D then 36-40 in Column E.
当我运行这个宏时,值 1-6 放入 A 列,然后 7-15 放入 B 列,16-26 放入 C 列,然后 27-35 放入 D 列,然后 36-40 放入 E 列.
What I expected was that values 1-10 go into column A, 11-20 into column B, 21-30 into column C and 31-40 into column D.
我期望的是值 1-10 进入 A 列,11-20 进入 B 列,21-30 进入 C 列,31-40 进入 D 列。
I'm used to C and C++ where if I divide an integer by another integer, the result is calculated using integer maths. What's different in VBA?
我习惯了 C 和 C++,如果我将一个整数除以另一个整数,结果是使用整数数学计算的。VBA 有什么不同?
回答by Daniel
To use "integer maths" for division use \
instead of /
.
要使用“整数数学”进行除法\
而不是/
.
Integer Division \
Dividing an item means cutting it in pieces or fractions of a set value. Therefore, the division is used to get the fraction of one number in terms of another. The Visual Basic language provides two types of operations for the division. If you want the result of the operation to be a natural number, called an integer, use the backlash operator "\" as the divisor. The formula to use is:
Value1 \ Value2
This operation can be performed on two types of valid numbers, with or without decimal parts. After the operation, the result would be a natural number.
Decimal Division /
The second type of division results in a decimal number. It is performed with the forward slash "/". Its formula is:Value1 / Value2
After the operation is performed, the result is a decimal number.
整数除法 \
分割项目意味着将其切割成设定值的碎片或分数。因此,除法用于得到一个数相对于另一个数的分数。Visual Basic 语言为除法提供了两种类型的运算。如果您希望运算的结果是一个自然数,称为整数,请使用反冲运算符“\”作为除数。使用的公式是:
Value1 \ Value2
可以对两种类型的有效数字执行此操作,带或不带小数部分。运算后,结果将是一个自然数。
Decimal Division /
第二种除法产生十进制数。它使用正斜杠“/”执行。其公式为:Value1 / Value2
运算完成后,结果为十进制数。
回答by Jon Crowell
The back slash is used for integer division, and the forward slash for decimal division. Change your forward slash to a back slash and you should get the result you are expecting.
反斜杠用于整数除法,正斜杠用于十进制除法。将正斜杠更改为反斜杠,您应该得到预期的结果。