VBA function to find number of decimal places

chirp

Active Member
Joined
Nov 17, 2011
Messages
338
Hey,

I need a function to find the number of decimal places of a certain number (in this specific case doubles)

The first solution would be something like this:

Code:
'returns the number of decimal places within a double
Public Function getDecPlaces(inputNum As Double) As Long
Dim ndx As Long
ndx = InStr(1, inputNum, ".")
If ndx > 0 Then
    getDecPlaces = Len$(CStr(inputNum)) - ndx
End If
End Function

But i feel there is likely a much better way of doing this...any help would be appreciated!!
 
rick..most likely!

mike...Thanks that is very cool and fast! i will use that approach in my function!
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
another solution would be to use the strReverse function which, with the short ~15 length strings is relatively fast...this results in a simple and fast method as in:

Code:
'Returns number of decimal places from a double
Public Function getDecPlaces(tDub As Double) As Long
    getDecPlaces = InStr(1, StrReverse(CStr(tDub)), ".")
    getDecPlaces = getDecPlaces - Abs(CLng(getDecPlaces <> 0))
End Function
 
Upvote 0
I don't know for sure, but I think this UDF should be quite fast...

Code:
Function AfterPoint(S As String) As Long
  Dim X As Byte, B() As Byte
  B = S
  For X = 1 To UBound(B)
    If B(X) = 46 Then
      AfterPoint = Len(S) - 1 - X / 2
      Exit Function
    End If
  Next
End Function
 
Upvote 0
i think that the byte array method is good, but just copying the input into the byte array takes roughly the same time as the other methods (on my computer). in general i find working with byte arrays usually is faster but not necessarily for such small strings
 
Upvote 0

Forum statistics

Threads
1,216,116
Messages
6,128,933
Members
449,480
Latest member
yesitisasport

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top