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
Solved

Checking the number of decimal numbers

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

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…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

840 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