Excel 2010 VBA 反斜杠 (\) 运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10779091/
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
Excel 2010 VBA Backslash (\) Operator
提问by ObiObi
In learning VBA, every source I read tells me that the \
operator performs integer division. So I was thinking I could use it to extract the integer part of a floating point number.
在学习 VBA 的过程中,我阅读的每个来源都告诉我\
运算符执行整数除法。所以我想我可以用它来提取浮点数的整数部分。
I have a spreadsheet like this:
我有一个这样的电子表格:
A = 5
B = 50%
I need to compute: C = A x B
, rounded down to the nearest integer (ie, any 2.01, 2.1, 2.9, 2.99 etc. all should be rounded down to 2).
我需要计算:C = A x B
,向下舍入到最接近的整数(即,任何 2.01、2.1、2.9、2.99 等都应向下舍入为 2)。
I was thinking I could do this:
我以为我可以这样做:
C = (A * B) \ 1
But it doesn't work - it appears to me that the float part is rounded up (i.e., 2.25 is taken to 3) before performing the integer division.
但它不起作用 - 在我看来,浮点部分在执行整数除法之前被四舍五入(即,2.25 被取为 3)。
I have right now done this:
我现在已经这样做了:
C = (A * B - 0.5) \ 1
which doesn't work in some cases (where A * B
is exactly equal to an integer, likely representation issues).
在某些情况下不起作用(其中A * B
完全等于整数,可能是表示问题)。
What else can I do?
我还可以做些什么?
回答by ObiObi
Duh. It appears I could have done:
呃。看来我可以这样做:
Int(A * B)
整数(A * B)
to get what I wanted.
得到我想要的。
回答by aldo.roman.nurena
be careful:
当心:
any 2.01, 2.1, 2.9, 2.99 etc. all should be rounded down to 3
Then you are not looking for the nearest integer
, but the ceiling function. Nearest integer sounds like Round or Fix functions (you have homework now: look for the differences between them both!)
那么你不是在寻找nearest integer
,而是天花板功能。最近的整数听起来像 Round 或 Fix 函数(你现在有作业:寻找它们之间的差异!)
For the ceiling function, take a look here:
对于天花板功能,请看这里:
Ceiling function used as in Excel:
Excel 中使用的天花板函数:
http://www.excelforum.com/excel-programming/612151-how-to-use-standard-excel-function-in-vba.html
http://www.excelforum.com/excel-programming/612151-how-to-use-standard-excel-function-in-vba.html
Code for VBA ceiling function:
VBA天花板函数的代码: