Solved

Simple VB Question

Posted on 2003-10-29
3
163 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
3 Comments
 
LVL 3

Accepted Solution

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now