Solved

Checking the number of decimal numbers

Posted on 2006-06-24
3
157 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
Comment Utility
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
Comment Utility
Works like a charm. Thanks.
0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
Emanuele_Ciriachi,

You're most welcome :)

Regards,

Patrick
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

763 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

9 Experts available now in Live!

Get 1:1 Help Now