What is the JavaScript Math.Floor()
equivalent in VBA?. this function should remove all the decimal places and return only an integer.
The TRUNC function simply truncates (i.e. removes) decimal values if they exist – it doesn't do any rounding. The TRUNC function returns the integer portion of the number which is then subtracted from the original value.
To remove Decimals in Excel Formula Bar Using Decrease Decimal Button, execute the following steps. 📌 Steps: Select the range of cells or the entire column. Go to the Home tab > the Number group > Decrease Decimal button.
Right click the selected cells, and select the Format Cells from the right-clicking menu. 3. In the coming Format Cells dialog box, go to the Number tab, click to highlight the Number in the Category box, and then type a number in the Decimal Places box.
Of what i remember use the Int() function. ex
int(2.99) = 2 ; int(2.1)=2
and so on.
be careful that CInt() actually rounds the number, but Int() doesn't.
CInt(1.6) ~~ 2
Int(1.6) ~~ 1
It's 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.
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
The floor() method rounds a number DOWNWARDS to the nearest integer, and returns the result.
'~~> 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()
1 ~~ 0
0 ~~ 0
5 ~~ 5
5 ~~ 5
-5 ~~ -6
-6 ~~ -6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With