iOS Question Formatting text

Andrew Lindsay

Member
Licensed User
Longtime User
I would like to format my calculations in scientific notation, for example, 10023 I would like to show as 1.00e+04.

Likewise, I would like to be able to format the output to a fixed number of decimal places, without the exponent, for the sake of financial calculations etc.

Thanks for your assistance

Andrew
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is similar to B4A / B4J. You can use NumberFormat or NumberFormat2 or smart string literal to set the number of integer and fraction digits.

You can use this sub to format it in scientific notation (will also work in B4A and B4J)
B4X:
Sub ToScientificNotation(d As Double) As String
   Dim n As Int = Logarithm(d, 10)
   Dim m As Double = d / Power(10, n)
   Dim sb As StringBuilder
   sb.Initialize
   sb.Append(NumberFormat2(m, 1, 2, 2, False)).Append("e")
   If n >= 0 Then sb.Append("+")
   sb.Append(n)
   Return sb.ToString
End Sub
 
Upvote 0
Top