B4J Question Double division

jayel

Active Member
Licensed User
Longtime User
Hello,

This code :
Code: Copy
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Button1 As Button
    Private Label1 As Label
    Private privValue As Double
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.RootPane.LoadLayout("mainlayout") 'Load the layout file.
    privValue = 0
    MainForm.Show
End Sub

Sub Button1_Action
    Dim addvalue As Int = 10
    privValue = privValue + (1 / addvalue)
   
    Label1.Text = privValue
    Log(privValue)
   
End Sub

when I click the button i get :
Waiting for debugger to connect...
Program started.
0.1
0.2
0.30000000000000004
0.4
0.5
0.6
0.7
0.7999999999999999
0.8999999999999999
0.9999999999999999
1.0999999999999999
1.2
1.3
1.4000000000000001
1.5000000000000002
1.6000000000000003
1.7000000000000004
1.8000000000000005
1.9000000000000006
2.0000000000000004
2.1000000000000005
2.2000000000000006
2.3000000000000007
2.400000000000001
2.500000000000001

Why? I want to get :

0.1
0.2
0.3
0.4
0.5
0.6
.
.
.
2.2
2.3

Somebody help !

Greets

John
 
Decimal numbers cannot be expressed exactly in binary form.
Instead of Label1.Text = privValue
use Label1.Text = NumberFormat(privValue, 1, 1)
0.9
1
1.1

EDIT according to post #8.
To force the display of 1 as 1.0
use Label1.Text = NumberFormat2(privValue, 1, 1, 1, False)
0.9
1.0
1.1
 
Last edited:
Upvote 0
Yes,

But when I change my addvalue to 50 ???
Your solution won't work

Not logic the results?
 
Upvote 0
Sub Button1_Action
Dim addvalue As Int = 10
privValue = privValue + 1
Log(privValue/addvalue)
End Sub
 
Upvote 0
@Daestrum

Your code will not avoid a display like 0.30000000000000004 !

Instead of
Code: Copy
Sub Button1_Action
    Dim addvalue As Int = 10
    privValue = privValue + (1 / addvalue)
  
    Label1.Text = NumberFormat(privValue, 1, 1)
    Log(NumberFormat(privValue, 1, 1))
End Sub

This would be more efficient no need to make the division every time:
Code: Copy
Sub Button1_Action
    Dim addvalue As Double = 0.1
    privValue = privValue + addvalue
  
    Label1.Text = NumberFormat(privValue, 1, 1)
    Log(NumberFormat(privValue, 1, 1))
End Sub
 
Upvote 0
You should use NumberFormat2:
Code: Copy
NumberFormat2(privValue, 1, 1, 1, False)

This way integer numbers will be shown with a .0 behind:
...
0.5
0.6
0.7
0.8
0.9
1.0 '<--
1.1
...
 
Upvote 0
As addValue is normally dynamic value from a database I had to to this the make it work :
Code: Copy
    Dim addvalue As Int = 50
    privValue = privValue + (1 / addvalue)
    Dim strprivalue  As String = NumberFormat(privValue, 1, 4)
    Label1.Text = strprivalue
    Log(strprivalue)


This code was for testing only, addValue is a value that is retrieved from a table in a database
 
Upvote 0
Back
Top