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
Your code will not avoid a display like 0.30000000000000004 !
Instead of
B4X:
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:
B4X:
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
As addValue is normally dynamic value from a database I had to to this the make it work :
B4X:
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