B4J Question wrong formatting applied with math in advanced strings?

sorex

Expert
Licensed User
Hello,

It seems that the "no formatting" method is being overruled when math is being applied in the advanced strings method.

I noticed it when removing recalculated grid coordinates from a map didn't seem to work.

B4X:
Log($"d0=${data2(0)} - d1:${data2(1)} - sy:${sy} - sx=${sx} dy=${data2(0)-sy} - dx:${data2(1)-sx}"$)

Log($"d0=$1.0{data2(0)} - d1:$1.0{data2(1)} - sy:$1.0{sy} - sx=$1.0{sx} dy=$1.0{data2(0)-sy} - dx:$1.0{data2(1)-sx}"$)
log:

d0=4 - d1:4 - sy:4 - sx=4 dy=0.0 - dx:0.0
d0=4 - d1:4 - sy:4 - sx=4 dy=0 - dx:0

notice the 0.0 on the substracted values
 

Erel

Administrator
Staff member
Licensed User
I don't see any bug here. If you don't specify the number of fraction points like you did on the second line then the format cannot be assumed. You should never convert numbers to string without using specifically formatting the number with NumberFormat, $x.y or B4XFormatter.
 

sorex

Expert
Licensed User
the numbers are plain INTs. there's no need to add .0 to it as I didn't pick $1.1{} so the assumed format is just 1.0 as I use ${} (no formatting).
 

sorex

Expert
Licensed User
see it as abs(0-1) would still give -1. the outer command is ignored (not removing the 0.0 formatting)
 

Erel

Administrator
Staff member
Licensed User
You should never convert numbers to string without specifically formatting the number with NumberFormat, $x.y{} or B4XFormatter.
Don't assume the default format. It can change based on all kinds of parameters.
 

sorex

Expert
Licensed User
sorry but I don't see the relevance as the ${} no format happends last in the "operation"

I select no formatting and there is formatting applied. it should only be there when it is for example 0.5

no formatting means to me removing formatting if there is any on INTs and not adding some unwanted one.
 

Erel

Administrator
Staff member
Licensed User
BTW, the output of this code is 0 not 0.0:
B4X:
Dim sx, sy As Int
Dim data2(10) As Int
Log($"d0=${data2(0)} - d1:${data2(1)} - sy:${sy} - sx=${sx} dy=${data2(0)-sy} - dx:${data2(1)-sx}"$)
Still it is a programming mistake to omit the formatting specification as the default formatting might not be what you expect. It is also very simple to do.
 

sorex

Expert
Licensed User
data2() is strings because data comes from a regex.split

that there are differences between the old fashioned method and the advanced one can be seen with this code and only the ${} method adds formatting.
putting the blame on a coding mistake would make the old fashioned method format wrong aswell but it doesn't as it covers something that is missing in ${}.
both method should output the same thing that's what I'm trying to say.

B4X:
Dim v1 As String=2
Dim v2 As Int=1
Log(v1-v2)
Log("v:"& (v1-v2))
Log($"v:${v1-v2}"$)
Waiting for debugger to connect...
Program started.
1
v:1
v:1.0
 

Erel

Administrator
Staff member
Licensed User
Moved to the questions forum.

data2() is strings because data comes from a regex.split
When a string is parsed to a number it can be converted to a double. You shouldn't assume that it will be converted to an int just because the number is a whole number.

Bottom line, which was written 5 times already, use the format specifier. It is very simple.
 
Top