VB6: Forex Margin Calculator
Creating a math prog that is trying to refine the value of the Lots text box.
Balance = 19.00
Leverage = 50
Lots = 1.0
If Balance = 19 then a for statement must decrease Lots until Lots x Leverage equals Balance. What is happening is for statement is decreasing Lots by 0.01 and Lot of 1.0 should reach .38 and change text box value to .38 and exit sub. What is happening is when lots equals .43 its saying .42999999999.
Is there something wrong with my code?
Dim Balance, Lots, Margin As Double
Dim Leverage As Integer
Const Mini = 50 '50.00
Const Standard = 100 '100.00
Private Sub cmdCalculate1_Click()
Dim i As Integer
'--- Memory ---
Lots = txtLots1.Text
Balance = txtBal1.Text
If optMini1.Value = True Then Leverage = 50
If optStandard1.Value = True Then Leverage = 100
'--- FindLots ---
If chkFindLots.Value = 1 Then
Dim Fbal As Integer: Fbal = Balance
For i = 1 To 100
If Lots * Leverage = Fbal Then
txtLots1.Text = Lots
Exit Sub
End If
Lots = Lots - 0.01
Next i
End If
'If Balance < Leverage Then 'Margins
' MsgBox "Leverage of $" & Leverage & ".00 is not met": Exit Sub
'Else
' Margins = Balance / Leverage: lblMargins.Caption = Margins
'End If
End Sub
One issue is that some of your variables don't have the data type you think they do - for an explanation, see the article What's wrong with Dim x, y, z As Long? from our Classic VB FAQs(in the FAQ forum, which is shown near the top of our home page)
The main issue is the data type you are using, as Double and Single are both Floating Point, and are therefore focussed on speed (which is good) but not on accuracy (which can be very bad!). If accuracy is important to you, use the Currency data type instead.