Android Question less numbers after the decimal point in the answer. (Basic calculator)

MiguelCZ

Member
Licensed User
Longtime User
As you can see in the attached file. (the answer to a big multiplication).

instead of getting that amount of numbers after the "dot" I would like to just get something like this: 6.5916E35 just 3 to 4 numbers but also showing E35

is there a way of doing it?

I already tried limiting the amount of number the edittext can hold. but by doing so it also cut the answer. for example if I limit the amount of number of the edittext to 5 the answer would be: 6.5916 eliminating E35 and that is an incorrect answer.

Thanks
 

Attachments

  • calc.png
    calc.png
    381.6 KB · Views: 233

Mahares

Expert
Licensed User
Longtime User
Try this concept. It will work for you:
B4X:
Dim MyString As String="6.59163647839E35"
Dim strVar() As String=Regex.Split("E",MyString) 
Msgbox("My result is = " & NumberFormat(strVar(0),1,4) & "E" & strVar(1),"")  'displays: 6.59163E35
 
Upvote 0

MiguelCZ

Member
Licensed User
Longtime User
Try this concept. It will work for you:
B4X:
Dim MyString As String="6.59163647839E35"
Dim strVar() As String=Regex.Split("E",MyString)
Msgbox("My result is = " & NumberFormat(strVar(0),1,4) & "E" & strVar(1),"")  'displays: 6.59163E35


Thanks. but I do not know how to use that code. also I would like it to work not just for that number but for the answer of any calculation
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
The calculation will yield a value (Myresult) before it is plugged into the text box. You can then have this code:
B4X:
Dim MyResult As String
Dim strVar() As String=Regex.Split("E",MyResult)
EditText1.text=NumberFormat(strVar(0),1,4) & "E" & strVar(1) 'The text box will show the format: 9999E99
 
Last edited:
Upvote 0

MiguelCZ

Member
Licensed User
Longtime User
The calculation will yield a value (Myresult) before it is plugged into the text box. You can then have this code:
B4X:
Dim MyResult As String
Dim strVar() As String=Regex.Split("E",MyResult)
EditText1.text=NumberFormat(strVar(0),1,4) & "E" & strVar(1) 'The text box will show the format: 9999E99


it gave me an error( see attached files)

this is a part of my code

B4X:
Sub Globals
   
    Dim ans As String
    Dim strVar() As String=Regex.Split("E",ans)
   
End Sub


Sub Button12_Click  'equal button

If Sign = "*" Then
    ans = one * two
    EditText1.text = NumberFormat(strVar(0),1,4) & "E" & strVar(1)
    Stat = 1
    End If
 

Attachments

  • error.png
    error.png
    469.4 KB · Views: 229
  • error1.png
    error1.png
    126.9 KB · Views: 228
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
You can not do the Regex in Globals, it has no value. It must be used after you have an answer that needs corrected only. Your Button12_Click sub should look something like this:

B4X:
Sub Button12_Click
    Dim MyString As String
    MyString = "6.59163647839E35" 'This is where you must assign your answer to MyString, this is just a sample
    If MyString.Contains("E") Then
        Dim strVar() As String = Regex.Split("E", MyString)
        Log("My result is = " & NumberFormat(strVar(0), 1, 4) & "E" & strVar(1))
    Else
        Log("My result is = " & NumberFormat(MyString, 1, 4))
    End If
End Sub
 
Last edited:
Upvote 0

MiguelCZ

Member
Licensed User
Longtime User
You can not do the Regex in Globals, it has no value. It must be used after you have an answer that needs corrected only. Your Button12_Click sub should look something like this:

B4X:
Sub Button12_Click
    Dim MyString As String
    MyString = "6.59163647839E35" 'This is where you must assign your answer to MyString, this is just a sample
    If MyString.Contains("E") Then
        Dim strVar() As String = Regex.Split("E", MyString)
        Log("My result is = " & NumberFormat(strVar(0),1,4) & "E" & strVar(1))
    Else
        Log("My result is = " & MyString)
    End If
End Sub


thank you so much, it is working now. thanks a lot magret and Mahares for helping me
 
Upvote 0
Top