Solved

Checking the number of decimal numbers

Posted on 2006-06-24
3
160 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…

813 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

10 Experts available now in Live!

Get 1:1 Help Now