Android Question Replace doesn't work in number transformation with decimals

DALB

Active Member
Licensed User
Hello,

Finally, I love numbers, but they don't love me !

In this little code:

B4X:
If s.contains(",") Then s.Replace(",",".")

I wish to transform 48,39 to 48.39.

So, the answer of the debugger is:
java.lang.NumberFormatException: Invalid double: "48,39"

But this number would be 48.39

Something is wrong or am I wrong ?

Thanks
 

DALB

Active Member
Licensed User
even if I use indexOf, the debugger tells me the same thing !!!
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
So you’re saying that line of code produces that error message? How? What is s? A string variable? A double? What?
 
Upvote 0

DALB

Active Member
Licensed User
Thanks for answering OliverA

S is here

B4X:
File.WriteList(File.DirDefaultExternal,fs,lst)
    lst=File.ReadList(File.DirDefaultExternal , fs)
    s=lst.Get(0)
If s.contains(",") Then s.Replace(",",".")
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
What did you declare (dim/private/public what?) s as?
 
Upvote 0

DALB

Active Member
Licensed User
Hooo yes, Xcuse me, s is a string

B4X:
Dim s As String
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
What is the output of
B4X:
File.WriteList(File.DirDefaultExternal,fs,lst)
lst=File.ReadList(File.DirDefaultExternal , fs)
log($"lst.Get(0): ${lst.Get(0)}"$)
s=lst.Get(0)
Log($"s: ${s}"$)
If s.contains(",") Then s.Replace(",",".")
 
Upvote 0

DALB

Active Member
Licensed User
S takes the value of a latitude , a point on a sphere.

I use for instance 48,35° (normaly 48.35°) to calculate a time on a meridian line.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
That's not what I was asking, and I forgot one more log statement:
B4X:
File.WriteList(File.DirDefaultExternal,fs,lst)
lst=File.ReadList(File.DirDefaultExternal , fs)
log($"lst.Get(0): ${lst.Get(0)}"$)
s=lst.Get(0)
Log($"s: ${s}"$)
If s.contains(",") Then s.Replace(",",".")
Log($"s: ${s}"$)
 
Upvote 0

DALB

Active Member
Licensed User
the output remains 48,39 not 48.39

but if I do this code:

B4X:
Dim s As String=""
    s=lst.Get(0)
    Dim i As Int=s.indexOf(",")
    Dim s1 As String=s.SubString2(0,i)
    Dim s2 As String=s.SubString2(i+1,s.length)
   
    s=s1 & "." & s2

it seems to work. I can add a number without any problem.
 
Upvote 0

DALB

Active Member
Licensed User
can be it due to the phone S5 of Samsung, or from an other origin ? I don't know !
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I'm such a fool/idiot! Strings are immutable!!!!
B4X:
If s.contains(",") Then 
  s = s.Replace(",",".")
End If
 
Upvote 0

DALB

Active Member
Licensed User
OliverA

What do you mean by immutable ?
In some Forums, we can see that strings can be changed with the Replace function !
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
What OliverA means by immutable is that this is wrong in post #10:
If s.contains(",") Then s.Replace(",",".")

but this is correct in post#12
s = s.Replace(",",".")
 
Upvote 0

DALB

Active Member
Licensed User
Wooo, yes, what a mistake from me !
More than 2 eyes is really usefull !

Thanks to everyone !
 
Upvote 0
Top