How to convert float/double to string.

mramos

Member
Licensed User
Longtime User
I see ToOct, ToHex, etc. I have a double I need to append to a string.

Dlat and DLon are strings, nLat and nLon are numbers. I looked, just do not see it.

B4X:
      nLat = Lat.SubString(Lat.IndexOf(":")+1) /60
      nLon = Lon.SubString(Lon.IndexOf(":")+1) /60
      DLat = DLat & nLat.substring(2)
      DLon = DLon & nLon.substring(2)
 

mramos

Member
Licensed User
Longtime User
So the above should be working?

I got it working by moving each one in temp (string), did what I needed and appended them. Works good, now I know just load a number to a string then work on it.

Thanks again.
 
Last edited:
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
is there a way to append a $ in front of a double. I tried everything including...

Text = 100.00
Text = "$" + Text


no luck with any of it :-/

Please help
 
Upvote 0

netchicken

Active Member
Licensed User
Longtime User
Try Round2
Basic4android - Core

Round2 (Number As Double, DecimalPlaces As Int) As Double
Rounds the given number and leaves up to the specified number of fractional digits

Round2(3.1452567567878, 3)
 
Upvote 0

SlavaVB

Member
Licensed User
Longtime User
i just remembered that the "&" character appends a string

is there a way to catch exception for when a user enters a string instead of a double into a text box?
 
Upvote 0

wheretheidivides

Active Member
Licensed User
Longtime User
Try Round2
Basic4android - Core

Round2 (Number As Double, DecimalPlaces As Int) As Double
Rounds the given number and leaves up to the specified number of fractional digits

Round2(3.1452567567878, 3)

Could someone give a coded example please? I have a variable called "VAR" that is a double. It has a ton of spaces but i want it to truck to 2 places a decimal and 2 places.

Example: 21.9835458493
would like 21.98

This is my actual code that does not work. I am trying to put in the label for display.
TempNumber = (CashHourRate * CashHrs) + (TournHourRate * TournHrs) + (SuperHourRate * SuperHrs) + (CashHourRate * FreeTimeMins)
Round2(TempNumber, 3) As Double
Label1706dd.Text = Round2



here's my own answer
TempNumber = (CashHourRate * CashHrs) + (TournHourRate * TournHrs) + (SuperHourRate * SuperHrs) + (CashHourRate * FreeTimeMins)
TempNumber2 = Round2(TempNumber, 2)
Label1706dd.Text = "$ " & TempNumber2
 
Last edited:
Upvote 0

John H. Guillory

Member
Licensed User
Longtime User
Basic4android converts strings to numbers and vice versa as required.
You can always force a conversion by assigning the value to a variable of the target type:
B4X:
Dim s As String
s = nLat
s.SubString(...)

I've tried that in my program, and even went so far as doing the following to try to convert the double to a string.:

B4X:
Sub f3Frequency_TextChanged (Old As String, New As String)
Dim a As Float
Dim b As String

    If f3Frequency.Text.IndexOf(".") > 0 Then
      a = 468 / f3Frequency.Text * 12
      b = NumberFormat2(a,1,2,2,True)
      b = b + " inches"     
      f3leg.Text = b
    Else
      f3leg.Text = "Waiting on frequency"
    End If
End Sub

When debugging on the emulator, I get an exception on the line reading:

b = b + "inches"

stating that "inches" is an invalid 'double'. I can't understand this because b is a string, NumberFormat2 returns a string (supposedly), and yet it's depreciating the b variable to a double and complaining that my string concatenation doesn't act right.
 
  • Like
Reactions: Opa
Upvote 0

John H. Guillory

Member
Licensed User
Longtime User
The concatenation operator is &:
B4X:
b = b & " inches"
Thanks, that helped a Lot! It was racking my brains, because a previous post said if you want to force a type, declare it as that type. I know Basic4Android converts to Java, and NSBasic/App converts to Javascript, which are both fairly similiar. I know a bit of Javascript and Java, mainly where they're similiar to C++, but Java is still not my language of choice. Folks put down BASIC for being so simple, but the way I see it. I write software at times on a freelance basis. I write applications for myself that I can use, and sometimes for friends. Why use another language because it's popular, supposed to be super powerful, etc. when I can use a language that allows me to pop out applications quickly that do what I want it to do reliably? I personally Love Pascal, and still use Virtual Pascal for some things. I've seen a pascal compiler (Pe'pe') for Android that is console only. It's good, but couldn't see using it for anything productive, and console only apps aren't too popular for smart phones! I remember on NSBasic, you could concat anything to pretty much anything using the & operator. Perhaps what threw me off, B4A seems so much more like Visual BASIC, that it just didn't seem like it'd work there too.... ;) Right now, I'm sorta trying to learn as much of the language as possible, and convert my existing apps, adding features that where hard to do in NsBasic (Strangely enough, basic graphics was kinda limited! You had to get the device context of a panel, create a canvas (much like here), then write your code with no auto-completion of the actual graphic commands, and no real documentation of what graphic commands where available! To find out what was available, you had to travel to the webkit documentation on another site, find what commands they allowed and use them with your program. You had no confirmation if the command was right or not until you ran the program. If the command was wrong, your program wouldn't run at all. If the command was right, it would. But there was no circle command, only an arc command, and I couldn't get the stupid thing to draw an arc from 0 to 359 for nothing. Even if I could, you had to moveTo(x,y), then when you drew the arc, it'd draw a line to the edge of the arc, draw the arc, then if you performed a lineTo(x,y), you'd have a pie piece. Not incredibly useful! Something else I've gotta say about Basic4Android, with NSBasic/App, I had to send my app to phonegap, send a keystore file to phonegap, unlock the app for the key, which was unlocked for 1 hour. I then had to re-build the app, then download the app to get the release file, then upload that to Android market and Amazon market. Whew! A lot of work. The only benefit was I also had a Windows Phone/Windows 8 app at the same time. With Basic4Android, I automatically get a signed app every time I test it out! Just upload it from my computer to the appstore! Incredibly easier! I gotta admit, that goes back to my original reason why I tend to stick to BASIC.
 
Upvote 0
Top