Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to Remove the Decimal Places

What is the JavaScript Math.Floor() equivalent in VBA?. this function should remove all the decimal places and return only an integer.

like image 952
Sami Al-Subhi Avatar asked May 17 '12 08:05

Sami Al-Subhi


People also ask

Which function remove the decimal portion of the value?

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.

How do I remove Decimals from Formula bar?

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.

How do I get Excel to only show 2 decimal places?

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.


3 Answers

Of what i remember use the Int() function. ex

int(2.99) = 2 ; int(2.1)=2 

and so on.

like image 186
Albi Patozi Avatar answered Sep 23 '22 05:09

Albi Patozi


be careful that CInt() actually rounds the number, but Int() doesn't.

CInt(1.6) ~~ 2
Int(1.6) ~~ 1
like image 42
user3849288 Avatar answered Sep 24 '22 05:09

user3849288


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

like image 41
Siddharth Rout Avatar answered Sep 21 '22 05:09

Siddharth Rout