Android Question Displaying Scientific notation and Double/Float

MrChemist

New Member
Licensed User
Longtime User
I'm a chemist and I'm just having trouble with seeing numbers the way I want them.

My numbers are hydrogen ion concentrations which are from 0.1 down to 10 to the -14 (10E-14) from a pH value, which is a number between roughly 0 and 14:
pH = -(pH)
concH = Power(10, pH)

My problem comes when I try to display the number. What I really want is something on the lines of:

Hydrogen ion concentration = 1.234E-5 (or similar)
but if I just put
Label3.Text = "Hydrogen ion concentration = " & concH 'yes it's mixed types and I shouldn't .....
then I get ALL of the decimal places, most of which are useless...... 1.2345678987654321E-5
I tried
Label3.Text = "Hydrogen ion concentration = " & NumberFormat(concH,4,6)
but then I get 0.000 for most of the numbers, as it converts to decimals like 0.00000123 and then truncates. Round() gave me pretty much the same problems.

Is there a built-in function or two that will do what I want, but I can't see it?
Or is it maybe in somebody's library (I can afford the paid-for version after payday)
Or am I going to have to write my own (not an issue, just time spent)

Sorry for the long question, chaps!
 

Mahares

Expert
Licensed User
Longtime User
Will this do it for you:
B4X:
Dim ph As Double=5.67
Dim concH As Double
ph = -(ph)
concH = Power(10, ph)
Dim strVar() As String=Regex.Split("E",concH)
Msgbox("Hydrogen ion concentration = " & NumberFormat(strVar(0),1,3) & "E" & strVar(1),"")  'displays:2.138E-6
 
Upvote 0

MrChemist

New Member
Licensed User
Longtime User
Will this do it for you:
B4X:
Dim ph As Double=5.67
Dim concH As Double
ph = -(ph)
concH = Power(10, ph)
Dim strVar() As String=Regex.Split("E",concH)
Msgbox("Hydrogen ion concentration = " & NumberFormat(strVar(0),1,3) & "E" & strVar(1),"")  'displays:2.138E-6

That was great, nearly there. The only problem it gave me was that larger tiny numbers containing no exponent, like 0.033, would throw a runtime error, since the "E" was not found and so strVar(1) was null.
I cured this by reading up about regex, modifying the example in the keywords wiki and making the final display of the values into:
B4X:
    Dim pH, concH, MpH As Double
    Dim Matcher1 As Matcher
    'Generates a random pH problem for A2 level pupils, maybe two, If I have a mind to do a strong acid AND a weak one.
    pH = Rnd(0,685) + 12
    pH = NumberFormat(pH/100,1,3)                       'a pH value between 0.12 and 6.97
    MpH = -(pH)
    concH = Power(10, MpH)

    Dim strVar() As String=Regex.Split("E",concH)
    Label1.Text = "For a strong acid, e.g. HCl:"
    Label2.Text = "pH = " & pH

Matcher1 = Regex.Matcher("E", concH)
    If Matcher1.Find Then 
        Label3.Text = "[H+] = " & NumberFormat(strVar(0),1,3) & "E" & strVar(1)
    Else 
        Label3.Text = "[H+] = " & NumberFormat(strVar(0),1,6)
    End If
Took 4 hours, on and off, and I still don't see how Matcher1 can be of type Matcher, which I haven't defined, unless that's secretly defined somewhere else. I'm accustomed to Delphi and C, coming back to Basic programming just to solve one problem. I'm sure I used to be able to do this formatting in one statement in Fortran in 1977. Maybe there are secrets and libraries yet to be discovered....

Many thanks for taking the time to help me and introduce me to the dark world of regex!
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
That is great that it you got it figured out. Just for your information, you could have also used the 'contains' instead of the 'matcher' like this:
B4X:
Dim pH, MpH As Double
Dim concH As String    'declared string
'Generates a random pH problem for A2 level pupils, maybe two, If I have a mind to do a strong acid AND a weak one.
pH = Rnd(0,685) + 12
pH = NumberFormat(pH/100,1,3) 'a pH value between 0.12 and 6.97
MpH = -(pH)
concH = Power(10, MpH)
If concH.Contains("E") Then
Dim strVar() As String=Regex.Split("E",concH)
Msgbox( "[H+] = " & NumberFormat(strVar(0),1,3) & "E" & strVar(1),"")
Else
Msgbox( "[H+] = " & NumberFormat(concH,1,6),"")
EndIf
 
Upvote 0
Top