Results 1 to 2 of 2

Thread: Possible format error?

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    1

    Possible format error?

    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

    Private Sub Form_Load()
    'Reclaim memory
    Lots = 0: ExtLots = 0: Leeway = 0: Balance = 0
    'txtBal1 = "": txtLots1 = "": lblLeeway = "": lblMargins = ""
    End Sub
    Attached Files Attached Files

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Possible format error?

    Welcome to VBForums

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width