vba 去除小数位的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10631992/
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
Function to Remove the Decimal Places
提问by Sami Al-Subhi
What is the JavaScript Math.Floor()
equivalent in VBA?. this function should remove all the decimal places and return only an integer.
Math.Floor()
VBA 中的 JavaScript等价物是什么?此函数应删除所有小数位并仅返回一个整数。
回答by Albi Patozi
Of what i remember use the Int() function. ex
我记得使用 Int() 函数。前任
int(2.99) = 2 ; int(2.1)=2
and so on.
等等。
回答by user3849288
be careful that CInt() actually rounds the number, but Int() doesn't.
请注意 CInt() 实际上对数字进行四舍五入,但 Int() 不会。
CInt(1.6) ~~ 2
Int(1.6) ~~ 1
回答by Siddharth Rout
It's Round()
它是 Round()
Sub Sample()
Dim dval As Double
dval = 1.12345
Debug.Print Round(dval, 0)
End Sub
0
above specifies the number of decimals you want.
0
以上指定您想要的小数位数。
EDIT:
编辑:
Albi Patozi is right. The equivalent
of Math.Floor()
is int()
. I was under the impression that you just wanted to return a number without the decimals. But then I looked up http://www.w3schools.com/jsref/jsref_floor.asp
阿尔比·帕托齐是对的。的equivalent
的Math.Floor()
是int()
。我的印象是你只想返回一个没有小数的数字。但后来我查了一下http://www.w3schools.com/jsref/jsref_floor.asp
The floor() method rounds a number DOWNWARDS to the nearest integer, and returns the result.
floor() 方法将数字向下舍入为最接近的整数,并返回结果。
'~~> JavaScript floor() Method
'var a=Math.floor(0.60); ~~> 0
'var b=Math.floor(0.40); ~~> 0
'var c=Math.floor(5); ~~> 5
'var d=Math.floor(5.1); ~~> 5
'var e=Math.floor(-5.1); ~~> -6
'var f=Math.floor(-5.9); ~~> -6
Sub Sample()
Dim dval(5) As Double, i As Long
dval(0) = 0.6: dval(1) = 0.4: dval(2) = 5
dval(3) = 5.1: dval(4) = -5.1: dval(5) = -5.9
For i = LBound(dval) To UBound(dval)
Debug.Print Round(dval(i), 0); " ~~ "; Int(dval(i))
Next
End Sub
RESULT
结果
ROUND() ~~ INT()
ROUND() ~~ INT()
1 ~~ 0
1~0
0 ~~ 0
0 ~~ 0
5 ~~ 5
5~5
5 ~~ 5
5~5
-5 ~~ -6
-5 ~~ -6
-6 ~~ -6
-6 ~~ -6
回答by ashleedawg
You could also call one of the Excel worksheet functions from VBA:
您还可以从 VBA 调用 Excel 工作表函数之一:
- Application.WorksheetFunction.Floor_Math
—— Application.WorksheetFunction.Floor_Math
- Application.WorksheetFunction.Floor
—— Application.WorksheetFunction.Floor
回答by Mark
For positive values, VBA offers Int()
and Fix()
which are functionally equivalent to Math.floor()
. However, for negative values, only Int()
is functionally equivalent to Math.floor()
. The VBA function Fix()
might be what you want if you are only interested in getting the value that is on the left side of the decimal point.
对于正值,VBA 提供Int()
和Fix()
在功能上等效于Math.floor()
. 但是,对于负值,仅Int()
在功能上等效于Math.floor()
。Fix()
如果您只想获取小数点左侧的值,则VBA 函数可能就是您想要的。
Math.floor(2.8) == 2
Int(2.8) == 2
Fix(2.8) == 2
Math.floor(2.8) == 2
Int(2.8) == 2
Fix(2.8) == 2
Math.floor(-2.8) == -3
Int(-2.8) == -3
Fix(-2.8) == -2
Math.floor(-2.8) == -3
Int(-2.8) == -3
Fix(-2.8) == -2
Int, Fix Functions
Returns the integer portion of a number.
Syntax
Int(number)
Fix(number)
The required number argument is a Double or any valid numeric expression. If number contains Null, Null is returned.
Remarks
Both Int and Fix remove the fractional part of number and return the resulting integer value.
The difference between Int and Fix is that if number is negative, Int returns the first negative integer less than or equal to number, whereas Fix returns the first negative integer greater than or equal to number. For example, Int converts -8.4 to -9, and Fix converts -8.4 to -8.
Fix(number) is equivalent to:
Sgn(number) * Int(Abs(number))
整数,固定函数
返回数字的整数部分。
句法
整数(数)
修复(数量)
所需的数字参数是 Double 或任何有效的数字表达式。如果 number 包含 Null,则返回 Null。
评论
Int 和 Fix 都删除 number 的小数部分并返回结果整数值。
Int 和 Fix 的区别在于,如果 number 为负数,则 Int 返回第一个小于或等于 number 的负整数,而 Fix 返回第一个大于或等于 number 的负整数。例如,Int 将 -8.4 转换为 -9,Fix 将 -8.4 转换为 -8。
Fix(number) 相当于:
Sgn(number) * Int(Abs(number))