Isolate decimal part of a number (1 Viewer)

Catalina

Registered User.
Local time
Yesterday, 19:20
Joined
Feb 9, 2005
Messages
462
I have number field (Double, 2 decimals)

I need to convert decimals such as:
0.15 to 0.25
6.30 to 6.50
9.70 to 9.75
etc.

I guess I need to do a replace but how do I extract the decimal part?
Or is there is better way?

Thanks.

Catalina
 

Catalina

Registered User.
Local time
Yesterday, 19:20
Joined
Feb 9, 2005
Messages
462
Thanks for the response Paul.

No, It is not a rounding problem.
I have to change values that are the result of calculations to certain other values.

It is part of a trucker's logbook where only .00, .25, .50 and .75 can be used.

Catalina
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:20
Joined
Aug 30, 2003
Messages
36,130
But isn't that rounding to .25? In any case, you can get the decimal portion with

TheNumber - Int(TheNumber)
 
Local time
Yesterday, 21:20
Joined
Mar 4, 2008
Messages
3,856
Seems like with a little bit of simple algebra, you could do this.

For instance:
Y/60 = X/100
becomes:
Y * 100/60 = X

One way to do this is to write a function in VBA to take the "Y" value and return the "X" value (warning: untested air code!):
Code:
Function ConvertMinutesToDecimal (Y as Double) as Double
ConvertMinutesToDecimal = (Y * 100)/60
End Function

Then just call the function when you need the conversion done. Be sure to multiply times .01 if you send in whole (un-decimal notated) minutes.
 

raskew

AWF VIP
Local time
Yesterday, 21:20
Joined
Jun 2, 2001
Messages
2,734
Hi -

Give this a try.

Code:
Public Function RoundTo(pItem, pfactor)
'Purpose: Round to the nearest pfactor
'input:   from immediate window: ? roundto(15.35, 0.25)
'output:  15.25
   RoundTo = (Int(pItem / pfactor + 0.5) * pfactor)
End Function

HTH - Bob
 

Catalina

Registered User.
Local time
Yesterday, 19:20
Joined
Feb 9, 2005
Messages
462
I may have found a solution.
fldElapsed holds the calculated value and then I call tConversion.

Code:
Public Sub tConversion()

Dim vrTime1 As Integer
Dim vrTime2 As Integer

vrTime1 = Int(Me.fldElapsed)
vrTime2 = Mid(Me.fldElapsed, InStr(Me.fldElapsed, ".") + 1)

If vrTime2 = "15" Then
    vrTime2 = "25"
    Me.fldElapsed = vrTime1 & "." & vrTime2
    
ElseIf vrTime2 = "3" Then
    vrTime2 = "50"
    Me.fldElapsed = vrTime1 & "." & vrTime2
    
ElseIf vrTime2 = "45" Then
    vrTime2 = "75"
    Me.fldElapsed = vrTime1 & "." & vrTime2
    
ElseIf vrTime2 = "55" Then
    vrTime2 = "25"
    Me.fldElapsed = vrTime1 & "." & vrTime2
    
ElseIf vrTime2 = "7" Then
    vrTime2 = "50"
    Me.fldElapsed = vrTime1 & "." & vrTime2
    
ElseIf vrTime2 = "85" Then
    vrTime2 = "75"
    Me.fldElapsed = vrTime1 & "." & vrTime2
    
End If

End Sub

Thanks for all the suggestions.

Catalina
 

RuralGuy

AWF VIP
Local time
Yesterday, 20:20
Joined
Jul 2, 2005
Messages
13,826
I found the math solutions *elegant*, but who am I?
 

petehilljnr

Registered User.
Local time
Yesterday, 19:20
Joined
Feb 13, 2007
Messages
192
What about 'borrowing' the best of the answers and rolling into one:

Code:
Function ConvTime(myTime As Double) As Double
 
Dim myMins As Double
Dim myDecTime As Double
 
myMins = myTime - Int(myTime)
myDecTime = Int(myTime) + ((myMins * 100) / 60) 'converts to decimal time
 
ConvTime = (Int(myDecTime / 0.25 + 0.5) * 0.25) 'rounds the decimal time
 
End Function

Regards,
Pete
 

Users who are viewing this thread

Top Bottom