?
Solved

Simple VB Question

Posted on 2003-10-29
3
Medium Priority
?
171 Views
Last Modified: 2010-05-01
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.

0
Comment
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
  • Learn & ask questions
3 Comments
 
LVL 3

Accepted Solution

by:
kenspencer earned 200 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

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


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
0
 

Expert Comment

by:clangl
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

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

Join & Ask a Question