• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

Checking the number of decimal numbers

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
Emanuele_Ciriachi
Asked:
Emanuele_Ciriachi
  • 2
1 Solution
 
Patrick MatthewsCommented:
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
 
Emanuele_CiriachiAuthor Commented:
Works like a charm. Thanks.
0
 
Patrick MatthewsCommented:
Emanuele_Ciriachi,

You're most welcome :)

Regards,

Patrick
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now