B4J Question wrong formatting applied with math in advanced strings?

Discussion in 'B4J Questions' started by sorex, Aug 15, 2019.

  1. sorex

    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.

    Code:
    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
     
  2. Erel

    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.
     
  3. sorex

    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).
     
  4. sorex

    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)
     
  5. Erel

    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.
     
  6. sorex

    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.
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    BTW, the output of this code is 0 not 0.0:
    Code:
    Dim sx, sy As Int
    Dim data2(10As 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.
     
  8. sorex

    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.

    Code:
    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
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Moved to the questions forum.

    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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice