Solved

Checking the number of decimal numbers

Posted on 2006-06-24
3
159 Views
Last Modified: 2010-04-30
Hi, I have a MSAccess application with VB code behind it, how do I check how many decimal numbers a numeric variable holds?

I tought I could multiply by 10/100/1000, remove the integer part and see if it's still higher than 0 - but how do I pick only the integer part of a number?
0
Comment
Question by:Emanuele_Ciriachi
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 128 total points
ID: 16976014
Hi Emanuele_Ciriachi,

I assume that you mean, if the variable currently holds the value 123.4567, you would want 4, which is the number of
characters to the right of the decimal point.

If this is so, try this UDF:

Function DecimalPlaces(OrigNumber As Variant)

    Dim arr As Variant
    Dim TestStr As String
    Dim TestLen As Long

    TestStr = CStr(OrigNumber)
    arr = Split(TestStr, ".")

    On Error Resume Next

    TestLen = Len(arr(1))

    If Err <> 0 Then TestLen = 0

    DecimalPlaces = TestLen

End Function



There are alternative approaches, such as:

    Len(MyNumber - Int(MyNumber)) - 2

but I advise against this: a floating point operations like this can lead to an apparent inflation of the number of
decimal places.  That is why I immediately converted the numeric value to a string.

Regards,

Patrick
0
 
LVL 1

Author Comment

by:Emanuele_Ciriachi
ID: 16976102
Works like a charm. Thanks.
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 16976509
Emanuele_Ciriachi,

You're most welcome :)

Regards,

Patrick
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb6 connector to SQL Server 2 34
Crystal reports - Formula Field code need assistance with code 17 51
TT Auto Dashboard 13 80
how to know if my Checkbox is True in VB6.0? 9 36
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…

929 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

13 Experts available now in Live!

Get 1:1 Help Now