Solved

# Simple VB Question

Posted on 2003-10-29
168 Views
I am not a VB expert and I am trying to make a simple function that will count the number of decimal places there is in a number.  This is what I've done:

Public Function countDecimalPlaces(number As Double) As Integer
Do While number > Math.Round(number)
number = number * 10
countDecimalPlaces = countDecimalPlaces + 1
Loop
End Function

The problem is that it only works for up to 5 digits because in the comparison line for the loop (Do While number > Math.Round(number)), the Math.Round(number) part makes an integer, thus the the 5 digits the function will work until.  I have tried a few other things for the right side of the inequation (ie: multiplying and dividing by powers of 10, using Int(number), etc.)  but got the same results.

I can't seem to cast the right side of the inequation into a double properly.

0
Question by:djschick
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 3

Accepted Solution

kenspencer earned 50 total points
ID: 9645271
Hi,
Why not something like this:

Public Function countDecimalPlaces(number as Double) As Integer
Dim sNum As String
dim x        As Integer

sNum = Cstr(number)
x = Instr(sNum,".")
If x > 0 then x = Len(sNum) - x
countDecimalPlaces = x
Exit Function

Ken
0

LVL 17

Expert Comment

ID: 9645544
I came up with the top method after realizing Ken basically did the bottom one. Darn skimming. :P

MsgBox CountDecimal(1.12345678)
End Sub
Private Function CountDecimal(ByVal Number As Double) As Integer
CountDecimal = Len(CStr(Number)) - Round(Number, 0) - 1
End Function

or...

MsgBox CountDecimal(1.123456789)
End Sub
Private Function CountDecimal(ByVal Number As Double) As Integer
Dim iPos As Integer
iPos = InStr(1, Number, ".")
If iPos > 0 Then
CountDecimal = Len(CStr(Number)) - iPos
Else
CountDecimal = 0
End If
End Function
0

Expert Comment

ID: 9649546
Public Function NumberOfCharInString(strSearchIn as string, strSerarchFor as string) as integer
dim intCnt as Integer, intSavePlace as Intger
intCnt=0
intSavePlace=1
Do while intSavePlace>0
intSavePlace=  Instr(intsavePlace, strSeachIn, strSearchFor)
If intSacePlace>0 then
intCnt=intCnt+1
end if
loop
NumberOfCharInString=intCnt
end function

Then you can count what ever you want in a string
by calling NumberOfCharInString ("LOOK.FOR.DECIMALS",".")
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
###### Suggested Courses
Course of the Month8 days, 6 hours left to enroll