Getting the number of decimal places from a number 
Author Message
 Getting the number of decimal places from a number

Is there a function that will return the  number of decimal places in a
number?

--
Wendy Gatanis, Product Manager
Distributive Software, Inc.
2300 Fall Hill Ave., Suite 100
Fredericksburg, VA 22401
540.372.4500  /  800.779.6306

http://www.*-*-*.com/



Sun, 07 Jul 2002 03:00:00 GMT  
 Getting the number of decimal places from a number
The number of digits after the decimal point can be found like this:

NumAsStr = CStr(YourNumber)
If NumAsStr Like "*.*" Then
   DecimalPlaces = Len(NumAsStr) - InStr(NumAsStr, ".")
Else
   DecimalPlaces = 0
End If

Rick


Quote:
> Is there a function that will return the  number of decimal places in a
> number?

> --
> Wendy Gatanis, Product Manager
> Distributive Software, Inc.
> 2300 Fall Hill Ave., Suite 100
> Fredericksburg, VA 22401
> 540.372.4500  /  800.779.6306

> http://www.distributive.com



Mon, 08 Jul 2002 03:00:00 GMT  
 Getting the number of decimal places from a number
For more international flare, you might want to check what the decimal
separator is, and test for that:

Private Function DecimalPlaces(Num#) As Long
Dim tmp$, sep$
    sep = Mid$(CStr(1 / 2), 2, 1)
    tmp = CStr(Num)
    DecimalPlaces = Len(Mid$(tmp, InStr(tmp, sep) + 1))
End Function

LFS

Quote:

> The number of digits after the decimal point can be found like this:

> NumAsStr = CStr(YourNumber)
> If NumAsStr Like "*.*" Then
>    DecimalPlaces = Len(NumAsStr) - InStr(NumAsStr, ".")
> Else
>    DecimalPlaces = 0
> End If

> Rick

> "wendy gatanis" wrote
> > Is there a function that will return the  number of decimal places in a
> > number?

> > --
> > Wendy Gatanis, Product Manager



Mon, 08 Jul 2002 03:00:00 GMT  
 Getting the number of decimal places from a number


Fri, 19 Jun 1992 00:00:00 GMT  
 Getting the number of decimal places from a number
It should have been:

Private Function DecimalPlaces(Num#) As Long
Dim tmp$, tst$, pos&
    tst = Mid$(CStr(1 / 2), 2, 1)
    tmp = CStr(Num)
    pos = InStr(tmp, tst)
    If pos Then DecimalPlaces = Len(Mid$(tmp, pos + 1))
End Function

Unfortunately I didn't test the case when there are no decimal numbers,
this will catch that....

LFS



Mon, 08 Jul 2002 03:00:00 GMT  
 Getting the number of decimal places from a number


Fri, 19 Jun 1992 00:00:00 GMT  
 Getting the number of decimal places from a number
Good point about the international decimal separator.

The expression

      Format$(0, ".")

will also return the decimal separator. (And for those non-believers out
there, yes, this *does* work for international settings -- try it before
responding that it doesn't.)

Modifying my original code yields:

    NumAsStr = CStr(YourNumber)
    Sep = Format$(0, ".")
    If NumAsStr Like "*" & Sep & "*" Then
       DecimalPlaces = Len(NumAsStr) - InStr(NumAsStr, Sep)
    Else
       DecimalPlaces = 0
    End If

Rick


Quote:
> For more international flare, you might want to check what the decimal
> separator is, and test for that:

> Private Function DecimalPlaces(Num#) As Long
> Dim tmp$, sep$
>     sep = Mid$(CStr(1 / 2), 2, 1)
>     tmp = CStr(Num)
>     DecimalPlaces = Len(Mid$(tmp, InStr(tmp, sep) + 1))
> End Function

> LFS


> > The number of digits after the decimal point can be found like this:

> > NumAsStr = CStr(YourNumber)
> > If NumAsStr Like "*.*" Then
> >    DecimalPlaces = Len(NumAsStr) - InStr(NumAsStr, ".")
> > Else
> >    DecimalPlaces = 0
> > End If

> > Rick

> > "wendy gatanis" wrote
> > > Is there a function that will return the  number of decimal places in
a
> > > number?

> > > --
> > > Wendy Gatanis, Product Manager



Mon, 08 Jul 2002 03:00:00 GMT  
 Getting the number of decimal places from a number
Rick,

Quote:
> Good point about the international decimal separator.

> The expression

>       Format$(0, ".")

> will also return the decimal separator. (And for those non-believers out
> there, yes, this *does* work for international settings -- try it before
> responding that it doesn't.)

> Modifying my original code yields:

>     NumAsStr = CStr(YourNumber)
>     Sep = Format$(0, ".")
>     If NumAsStr Like "*" & Sep & "*" Then
>        DecimalPlaces = Len(NumAsStr) - InStr(NumAsStr, Sep)
>     Else
>        DecimalPlaces = 0
>     End If

Somewhat more reliable on international issues, and much easier:

DecimalPlaces = Len(CStr(CDbl(YourNumber) - Fix(CDbl(YourNumber)))) - 2

Regards,

Harald M. Genauck

ABOUT Visual Basic - das Webmagazin
http://www.aboutvb.de



Mon, 08 Jul 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Exporting a 3 decimal number to text file produces trunc 2 decimal text number

2. Limit number of decimal places

3. numbers w/2 decimal places

4. numbers to 2 decimal places

5. Newbie : Trimming numbers (to 2 decimal places)

6. Textbox - Limiting Input to Numbers, decimal place and negative sign

7. Validating for number input with decimal places

8. Limiting Number of Decimal Places?

9. numbers w/2 decimal places

10. Number format Question ? (decimal place question)

11. v7 activex viewer uses default number of decimal places

12. Limiting the number of decimal places

 

 
Powered by phpBB® Forum Software