Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS DataTypes Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /DataTypes/BigCurrency
This example is the version from Thu, 13th Mar 2019.
Project "BigCurrency.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
EventHandler Sub Open()
// tests
Dim b As New BigCurrency(20)
Dim c As New BigCurrency(10)
dim cc as BigCurrency = -c
Dim d1 As BigCurrency = b + c
Dim e1 As BigCurrency = b - c
Dim f1 As BigCurrency = b * c
Dim g1 As BigCurrency = b / c
Dim h1 As BigCurrency = b \ c
Dim dd As Double = d1.doubleValue
Dim d2 As BigCurrency = b + 5
Dim e2 As BigCurrency = b - 5
Dim f2 As BigCurrency = b * 5
Dim g2 As BigCurrency = b / 5
Dim h2 As BigCurrency = b \ 5
Dim d3 As BigCurrency = 5 + c
Dim e3 As BigCurrency = 5 - c
Dim f3 As BigCurrency = 5 * c
Dim g3 As BigCurrency = 5 / c
Dim h3 As BigCurrency = 5 \ c
Dim d1s As String = d1.StringValue
Dim e1s As String = e1.StringValue
Dim f1s As String = f1.StringValue
Dim g1s As String = g1.StringValue
Dim h1s As String = h1.StringValue
Dim d2s As String = d2.StringValue
Dim e2s As String = e2.StringValue
Dim f2s As String = f2.StringValue
Dim g2s As String = g2.StringValue
Dim h2s As String = h2.StringValue
Dim d3s As String = d3.StringValue
Dim e3s As String = e3.StringValue
Dim f3s As String = f3.StringValue
Dim g3s As String = g3.StringValue
Dim h3s As String = h3.StringValue
// todo double
'Dim d4 As BigCurrency = b + 5.5
'Dim e4 As BigCurrency = b - 5.5
'Dim f4 As BigCurrency = b * 5.5
'Dim g4 As BigCurrency = b / 5.5
'Dim h4 As BigCurrency = b \ 5.5
'
'Dim d5 As BigCurrency = 5.5 + c
'Dim e5 As BigCurrency = 5.5 - c
'Dim f5 As BigCurrency = 5.5 * c
'Dim g5 As BigCurrency = 5.5 / c
'Dim h5 As BigCurrency = 5.5 \ c
Dim a As Currency = 5.5
Dim aa As New BigCurrency(a)
Dim d6 As BigCurrency = b + a
Dim e6 As BigCurrency = b - a
Dim f6 As BigCurrency = b * a
Dim g6 As BigCurrency = b / a
Dim h6 As BigCurrency = b \ a
Dim d7 As BigCurrency = a + c
Dim e7 As BigCurrency = a - c
Dim f7 As BigCurrency = a * c
Dim g7 As BigCurrency = a / c
Dim h7 As BigCurrency = a \ c
If c > a Then
// okay
Else
Break
End If
Break
End EventHandler
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class BigCurrency
ComputedProperty IntegerValue As Int64
Sub Get()
// no rounding!
Dim v As LargeNumberMBS = Self.value.Divide(factor)
Return v.Int64Value
End Get
End ComputedProperty
ComputedProperty StringValue As string
Sub Get()
Dim s As String = Me.value
Dim t As String
Dim minus As Boolean = False
If Left(s, 1) = "-" Then
minus = True
s = Mid(s,2)
End If
If Len(s) > Digits Then
t = Left(s, Len(s) - Digits) + "." + Right(s, Digits)
Else
t = s
While Len(t) < digits
t = "0" + t
Wend
t = "0."+t
End If
If minus Then
t = "-" + t
End If
Return t
End Get
End ComputedProperty
ComputedProperty doubleValue As double
Sub Get()
Dim d As Double = Self.value.DoubleValue
Return d / Factor
End Get
End ComputedProperty
Const CurrencyFactor = 10000
Const Digits = 8
Const Factor = 100000000
Sub Constructor()
// initialize with empty value
Self.value = New LargeNumberMBS
End Sub
Sub Constructor(value as BigCurrency)
If value = Nil Then
// initialize with empty value
Self.value = New LargeNumberMBS
Else
// copy value
Self.value = New LargeNumberMBS(value.value)
End If
End Sub
Sub Constructor(value as Currency)
Dim v As LargeNumberMBS = LargeNumberWithCurrencyValue(value)
Self.value = v.Multiply(Factor / CurrencyFactor)
End Sub
Sub Constructor(value as Double)
// todo
'value = value * Factor
'Self.value = LargeNumberMBS.NumberWithDouble(value)
End Sub
Sub Constructor(value as Integer)
Dim v As LargeNumberMBS = LargeNumberMBS.NumberWithInteger(value)
Self.value = v.Multiply(factor)
End Sub
Protected Sub Constructor(value as LargeNumberMBS)
Self.value = value
End Sub
Sub Constructor(value as string)
// todo
End Sub
Protected Shared Function LargeNumberWithCurrencyValue(value as Currency) As LargeNumberMBS
Static m As New MemoryBlock(8)
// get value out of currency, which gives value * 10000
m.CurrencyValue(0) = value
Dim n As Int64 = m.Int64Value(0)
Return LargeNumberMBS.NumberWithInt64(n)
End Function
Function Operator_Add(other as BigCurrency) As BigCurrency
Dim r As New BigCurrency
r.value = self.value + other.value
Return r
End Function
Function Operator_Add(other as Currency) As BigCurrency
Dim b As New BigCurrency(other)
Return Operator_Add(b)
End Function
Function Operator_Add(other as Integer) As BigCurrency
Dim o As LargeNumberMBS = LargeNumberMBS.NumberWithInteger(other)
Dim r As New BigCurrency
r.value = Self.value + o.Multiply(factor)
Return r
End Function
Function Operator_AddRight(other as BigCurrency) As BigCurrency
Dim r As New BigCurrency
r.value = other.value + self.value
Return r
End Function
Function Operator_AddRight(other as Currency) As BigCurrency
Dim b As New BigCurrency(other)
Return Operator_AddRight(b)
End Function
Function Operator_AddRight(other as Integer) As BigCurrency
Dim r As New BigCurrency
Dim o As LargeNumberMBS = LargeNumberMBS.NumberWithInteger(other)
r.value = o.multiply(factor) + self.value
Return r
End Function
Function Operator_Compare(other as BigCurrency) As Integer
Return Self.value.Operator_Compare(other.value)
End Function
Function Operator_Compare(other as Currency) As Integer
Dim b As New BigCurrency(other)
Return Operator_Compare(b)
End Function
Function Operator_Compare(other as Integer) As Integer
Dim o As LargeNumberMBS = LargeNumberMBS.NumberWithInteger(other)
Return Self.value.Operator_Compare(o.multiply(factor))
End Function
Function Operator_Convert() As Double
return self.doubleValue
End Function
Function Operator_Convert() As Int64
Return Self.IntegerValue
End Function
Function Operator_Convert() As String
return self.stringValue
End Function
Function Operator_Convert(value as Double) As BigCurrency
Return New BigCurrency(value)
End Function
Function Operator_Convert(value as Integer) As BigCurrency
Return New BigCurrency(value)
End Function
Function Operator_Convert(value as string) As BigCurrency
Return New BigCurrency(value)
End Function
Function Operator_Divide(other as BigCurrency) As BigCurrency
Dim r As New BigCurrency
r.value = Self.value.Multiply(factor) / other.value
Return r
End Function
Function Operator_Divide(other as Currency) As BigCurrency
Dim b As New BigCurrency(other)
Return Operator_Divide(b)
End Function
Function Operator_Divide(other as Integer) As BigCurrency
Dim r As New BigCurrency
Dim o As LargeNumberMBS = LargeNumberMBS.NumberWithInteger(other)
r.value = Self.value / o
Return r
End Function
Function Operator_DivideRight(other as BigCurrency) As BigCurrency
Dim r As New BigCurrency
r.value = other.value.Multiply(factor) / Self.value
Return r
End Function
Function Operator_DivideRight(other as Currency) As BigCurrency
Dim b As New BigCurrency(other)
Return Operator_DivideRight(b)
End Function
Function Operator_DivideRight(other as Integer) As BigCurrency
Dim r As New BigCurrency
Dim o As LargeNumberMBS = LargeNumberMBS.NumberWithInteger(other)
r.value = o / Self.value
Return r
End Function
Function Operator_IntegerDivide(other as BigCurrency) As BigCurrency
Dim r As New BigCurrency
// this will do integer divide and round down!
Dim v As LargeNumberMBS = Self.value / other.value
// multiply for 8 digits after decimal point
r.value = v.Multiply(factor)
Return r
End Function
Function Operator_IntegerDivide(other as Currency) As BigCurrency
Dim b As New BigCurrency(other)
Return Operator_IntegerDivide(b)
End Function
Function Operator_IntegerDivide(other as Integer) As BigCurrency
// this will do integer divide and round down!
Dim o As LargeNumberMBS = LargeNumberMBS.NumberWithInteger(other)
Dim r As LargeNumberMBS = Self.value / o
// multiply for 8 digits after decimal point
Return new BigCurrency(r)
End Function
Function Operator_IntegerDivideRight(other as BigCurrency) As BigCurrency
Dim r As New BigCurrency
// this will do integer divide and round down!
Dim v As LargeNumberMBS = other.value / self.value
// multiply for 8 digits after decimal point
r.value = v.Multiply(factor)
Return r
End Function
Function Operator_IntegerDivideRight(other as Currency) As BigCurrency
Dim b As New BigCurrency(other)
Return Operator_IntegerDivideRight(b)
End Function
Function Operator_IntegerDivideRight(other as Integer) As BigCurrency
Dim o As LargeNumberMBS = LargeNumberMBS.NumberWithInteger(other)
// this will do integer divide and round down!
Dim r As LargeNumberMBS = o / Self.value
Return New BigCurrency(r)
End Function
Function Operator_Modulo(other as BigCurrency) As BigCurrency
Dim r As New BigCurrency
r.value = Self.value.Multiply(factor) mod other.value
Return r
End Function
Function Operator_Modulo(other as Currency) As BigCurrency
Dim b As New BigCurrency(other)
Return Operator_Modulo(b)
End Function
Function Operator_Modulo(other as Integer) As BigCurrency
Dim o As LargeNumberMBS = LargeNumberMBS.NumberWithInteger(other)
Dim r As New BigCurrency
r.value = Self.value.Multiply(factor) mod o.Multiply(factor)
Return r
End Function
Function Operator_ModuloRight(other as BigCurrency) As BigCurrency
Dim r As New BigCurrency
r.value = other.value.Multiply(factor) mod self.value
Return r
End Function
Function Operator_ModuloRight(other as Currency) As BigCurrency
Dim b As New BigCurrency(other)
Return Operator_ModuloRight(b)
End Function
Function Operator_ModuloRight(other as Integer) As BigCurrency
Dim r As New BigCurrency
Dim o As LargeNumberMBS = LargeNumberMBS.NumberWithInteger(other)
r.value = o.Multiply(factor).Multiply(factor) Mod Self.value
Return r
End Function
Function Operator_Multiply(other as BigCurrency) As BigCurrency
Dim r As New BigCurrency
Dim t As LargeNumberMBS = Self.value * other.value
r.value = t.Divide(factor)
Return r
End Function
Function Operator_Multiply(other as Currency) As BigCurrency
Dim b As New BigCurrency(other)
Return Operator_Multiply(b)
End Function
Function Operator_Multiply(other as Integer) As BigCurrency
Dim o As LargeNumberMBS = LargeNumberMBS.NumberWithInteger(other)
Dim r As LargeNumberMBS = Self.value * o
Return new BigCurrency(r)
End Function
Function Operator_MultiplyRight(other as BigCurrency) As BigCurrency
Dim r As New BigCurrency
Dim t As LargeNumberMBS = other.value * Self.value
r.value = t.Divide(factor)
Return r
End Function
Function Operator_MultiplyRight(other as Currency) As BigCurrency
Dim b As New BigCurrency(other)
Return Operator_MultiplyRight(b)
End Function
Function Operator_MultiplyRight(other as Integer) As BigCurrency
Dim o As LargeNumberMBS = LargeNumberMBS.NumberWithInteger(other)
Dim r As LargeNumberMBS = o * Self.value
Return New BigCurrency(r)
End Function
Function Operator_Negate() As BigCurrency
Dim r As New BigCurrency
r.value = -Self.value
Return r
End Function
Function Operator_Subtract(other as BigCurrency) As BigCurrency
Dim r As New BigCurrency
r.value = Self.value - other.value
Return r
End Function
Function Operator_Subtract(other as Currency) As BigCurrency
Dim b As New BigCurrency(other)
Return Operator_Subtract(b)
End Function
Function Operator_Subtract(other as Integer) As BigCurrency
Dim o As LargeNumberMBS = LargeNumberMBS.NumberWithInteger(other)
Dim r As New BigCurrency
r.value = Self.value - o.Multiply(factor)
Return r
End Function
Function Operator_SubtractRight(other as BigCurrency) As BigCurrency
Dim r As New BigCurrency
r.value = other.value - Self.value
Return r
End Function
Function Operator_SubtractRight(other as Currency) As BigCurrency
Dim b As New BigCurrency(other)
Return Operator_SubtractRight(b)
End Function
Function Operator_SubtractRight(other as Integer) As BigCurrency
Dim o As LargeNumberMBS = LargeNumberMBS.NumberWithInteger(other)
dim r As New BigCurrency
r.value = o.Multiply(factor) - Self.value
Return r
End Function
Note "Readme"
BigCurrency class to replace Currency in Xojo and provide more digits precision
To change precision, please change factor and digits constants.
As we use UInt32 functions, please use maximum 9 digits.
Property Private value As LargeNumberMBS
End Class
End Project
The items on this page are in the following plugins: MBS DataTypes Plugin.