Article ID: 189862
Article Last Modified on 6/30/2004
Type MungeCurr
Value As Currency
End Type
Type Munge2Long
LoValue As Long
HiValue As Long
End Type
Dim C As MungeCurr, L As Munge2Long L.HiValue = Value1 L.LoValue = Value2 LSet C = L Value3 = C.Value
Dim C As MungeCurr, L As Munge2Long C.Value = Value1 LSet L = C Value2 = L.HiValue Value3 = L.LoValue
Private Function CurrToText(ByVal Value As Currency) As String
Dim Temp As String, L As Long
Temp = Format$(Value, "#.0000")
L = Len(Temp)
Temp = Left$(Temp, L - 5) & Right$(Temp, 4)
Do While Len(Temp) > 1 And Left$(Temp, 1) = "0"
Temp = Mid$(Temp, 2)
Loop
Do While Len(Temp) > 2 And Left$(Temp, 2) = "-0"
Temp = "-" & Mid$(Temp, 3)
Loop
CurrToText = Temp
End Function
Private Function TextToCurr(ByVal Value As String) As Currency
Dim L As Long, Negative As Boolean
Value = Trim$(Value)
If Left$(Value, 1) = "-" Then
Negative = True
Value = Mid$(Value, 2)
End If
L = Len(Value)
If L < 4 Then
TextToCurr = CCur(IIf(Negative, "-0.", "0.") & _
Right$("0000" & Value, 4))
Else
TextToCurr = CCur(IIf(Negative, "-", "") & _
Left$(Value, L - 4) & "." & Right$(Value, 4))
End If
End Function
Multiplying: C3 = (C1 * 10000) * C2
Dividing: C3 = (C1 / C2) / 10000
Multiplying: C2 = C1 * 24 Dividing: C2 = C1 / 3
Option Explicit
Private Type MungeCurr
Value As Currency
End Type
Private Type Munge2Long
LoValue As Long
HiValue As Long
End Type
Private Function TextToCurr(ByVal Value As String) As Currency
Dim L As Long, Negative As Boolean
Value = Trim$(Value)
If Left$(Value, 1) = "-" Then
Negative = True
Value = Mid$(Value, 2)
End If
L = Len(Value)
If L < 4 Then
TextToCurr = CCur(IIf(Negative, "-0.", "0.") & _
Right$("0000" & Value, 4))
Else
TextToCurr = CCur(IIf(Negative, "-", "") & _
Left$(Value, L - 4) & "." & Right$(Value, 4))
End If
End Function
Private Function CurrToText(ByVal Value As Currency) As String
Dim Temp As String, L As Long
Temp = Format$(Value, "#.0000")
L = Len(Temp)
Temp = Left$(Temp, L - 5) & Right$(Temp, 4)
Do While Len(Temp) > 1 And Left$(Temp, 1) = "0"
Temp = Mid$(Temp, 2)
Loop
Do While Len(Temp) > 2 And Left$(Temp, 2) = "-0"
Temp = "-" & Mid$(Temp, 3)
Loop
CurrToText = Temp
End Function
Private Sub Command1_Click()
Dim C1 As MungeCurr, C2 As MungeCurr, C3 As MungeCurr
Dim L As Munge2Long
' Convert a 64-bit value to two 32-bit values.
C1.Value = TextToCurr("123456789012345678")
LSet L = C1
Debug.Print CurrToText(C1.Value) & " => (" & L.HiValue & "," & _
L.LoValue & ")"
' Convert two 32-bit values to a 64-bit value.
L.HiValue = -1
L.LoValue = -1
LSet C1 = L
Debug.Print "(" & L.HiValue & "," & L.LoValue & ") => " & _
CurrToText(C1.Value)
' Add two pairs of 32-bit values and output the result as a pair
' of 32-bit values.
L.HiValue = 33333333
L.LoValue = 44444444
LSet C1 = L
L.HiValue = -22222222
L.LoValue = 11111111
LSet C2 = L
C3.Value = C1.Value + C2.Value
LSet L = C3
Debug.Print "(33333333,44444444) + (-22222222,11111111) => (" & _
L.HiValue & "," & L.LoValue & ")"
End Sub
123456789012345678 => (28744523,-1506741426)
(-1,-1) => -1
(33333333,44444444) + (-22222222,11111111) => (11111111,55555555)
172338 How To Use QueryPerformanceCounter to Time Code
129947 INFO: Win32 Replacement for the hmemcpy Function
189981 How To Seek Past VBA's 2Gb File Limit
Keywords: kbhowto KB189862