Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Sep 28, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Mathmatical Functions: Decimals

How can I read only from one side of the decimal in VB6? I need to get .4286 from a value of 22.4286. I also need to know how to get the integer value of 108.75 to read 108 and not round up to 109. Please help, I am lost.

There are two functions in VB to return the integer value of the number. These are Int and Fix. The difference between the two is that if the number you are using is negative, Int returns the first Integer value that is less than, or equal to, your number. Fix on the other hand returns the first negative integer greater than, or equal to, the number.

Both Int and Fix return the same values for positive numbers, the first integer value less than, or equal to, the number you pass.

There is no intrinsic function within VB to return the decimal portion of a number. The best you can do is create a function to do this for you. The easiest would just be to look for a decimal point and return anything after it like the following:

Private Function Dec(Number As Double) As Double
  Dim pos As Long

  pos = InStr(1, Number, ".")
  If pos > 0 Then
    Dec = Val(Mid(Number, pos))
    Dec = 0.0
  End If

End Function
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date