ComboBox load problem

Discussion in 'Questions (Windows Mobile)' started by mollin1, Apr 11, 2008.

  1. mollin1

    mollin1 New Member Licensed User

    Hi all,

    I'm using Basic4PPC V6.05 and want to load a ComboBox with a series of items (sample code is below).

    Sub App_Start
    Form1.Show
    For i = 1.01 To 12 Step 0.01
    If i = 1.45 Then
    ComboBox1.Add (i & " xxx")
    Else
    If i = 2.5 Then
    ComboBox1.Add (i & " xxx")
    Else
    ComboBox1.Add(i)
    End If
    End If
    Next i
    ComboBox1.SelectedIndex = 399 ' default display = 5 = index 399
    End Sub

    If I run the above inside the IDE its fine but when compiled to either device or desktop the combobox loads ok to "2.27" and the next value becomes "2.2799999999".

    The extra 0.009999999999's are then added to all subsequent items in the ComboBox.

    Any suggestions to solve this problem?

    Thanks in advance

    mollin1
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    You can use the Round method:
    Code:
    For i = 1.01 To 12 Step 0.01
    i = 
    Round(i,2)
    ...
     
  3. agraham

    agraham Expert Licensed User

    Use Round(i,2) to get the result you want. See Help->Main Help->Keywords->Math

    What you are seeing is due to the fact that binary floating point numbers used for general purposes in computers cannot always exactly represent non-integer real decimal numbers. Where exact precision is needed computer languages offer special numeric types such as "Currency" or "Decimal" where the results of operations using such numbers is guaranteed to be as expected.

    EDIT :- Damn, second again! :)
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    LOL
    However your answer is much more informative.
     
  5. mollin1

    mollin1 New Member Licensed User

    Many thanks for your high speed replies guys, both for the tip and the explanation...!

    I'll try it out now...
     
  6. mollin1

    mollin1 New Member Licensed User

    Erel, agraham,

    Success! thanks for your help.

    m
     
Loading...