Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Become a Premium Member and unlock a new, free course in leading technologies each month.

Solved

Posted on 2003-10-29

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.

Your help is much appreciated.

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.

Your help is much appreciated.

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
- Learn & ask questions

3 Comments

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

Private Sub Form_Load()

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...

Private Sub Form_Load()

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

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=intCn

end function

Then you can count what ever you want in a string

by calling NumberOfCharInString ("LOOK.FOR.DECIMALS",".")

Question has a verified solution.

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

Course of the Month1 day, 14 hours left to enroll

Earn Certification

CompTIA IT Fundamentals

$49.00$44.10

Join the community of 500,000 technology professionals and ask your questions.