Android Question Im having a problem when a number goes negative

cammel8

Member
Licensed User
Longtime User
I am making a flip counter and i have pictures that correspond to each number. everything works fine but when i try to have it go negative it crashes

here is the code:
B4X:
Dim p2TotalAsString As String
    p2TotalAsString=p2Total
    p2TotalAsString = (NumberFormat2(p2TotalAsString, 3, 0 ,0, False))
    
    Dim p2Totalfirstdigit, p2Totalseconddigit, p2Totalthirddigit As Int
    p2Totalfirstdigit =p2TotalAsString.substring2(0,1)
    p2Totalseconddigit =p2TotalAsString.substring2(1,2)
    p2Totalthirddigit =p2TotalAsString.substring2(2,3)
    
    Select p2Totalfirstdigit
        Case 0
            ivp2totalhun.Visible=False
        Case 1
            ivp2totalhun.Visible=True
            ivp2totalhun.Bitmap=LoadBitmap(File.DirAssets, "1c.png")
        Case 2           
            ivp2totalhun.Visible=True
            ivp2totalhun.Bitmap=LoadBitmap(File.DirAssets, "2c.png")
        Case 3
            ivp2totalhun.Visible=True
            ivp2totalhun.Bitmap=LoadBitmap(File.DirAssets, "3c.png")
        Case 4
            ivp2totalhun.Visible=True
            ivp2totalhun.Bitmap=LoadBitmap(File.DirAssets, "4c.png")
        Case 5
            ivp2totalhun.Visible=True
            ivp2totalhun.Bitmap=LoadBitmap(File.DirAssets, "5c.png")
        Case 6
            ivp2totalhun.Visible=True
            ivp2totalhun.Bitmap=LoadBitmap(File.DirAssets, "6c.png")
        Case 7
            ivp2totalhun.Visible=True
            ivp2totalhun.Bitmap=LoadBitmap(File.DirAssets, "7c.png")
        Case 8
            ivp2totalhun.Visible=True
            ivp2totalhun.Bitmap=LoadBitmap(File.DirAssets, "8c.png")
        Case 9
            ivp2totalhun.Visible=True
            ivp2totalhun.Bitmap=LoadBitmap(File.DirAssets, "9c.png")
        
    End Select
    Select p2Totalseconddigit
        Case 0
            ivp2totaltens.Bitmap=LoadBitmap(File.DirAssets, "0c.png")
        Case 1
            ivp2totaltens.Bitmap=LoadBitmap(File.DirAssets, "1c.png")
        Case 2           
            ivp2totaltens.Bitmap=LoadBitmap(File.DirAssets, "2c.png")
        Case 3
            ivp2totaltens.Bitmap=LoadBitmap(File.DirAssets, "3c.png")
        Case 4
            ivp2totaltens.Bitmap=LoadBitmap(File.DirAssets, "4c.png")
        Case 5
            ivp2totaltens.Bitmap=LoadBitmap(File.DirAssets, "5c.png")
        Case 6
            ivp2totaltens.Bitmap=LoadBitmap(File.DirAssets, "6c.png")
        Case 7
            ivp2totaltens.Bitmap=LoadBitmap(File.DirAssets, "7c.png")
        Case 8
            ivp2totaltens.Bitmap=LoadBitmap(File.DirAssets, "8c.png")
        Case 9
            ivp2totaltens.Bitmap=LoadBitmap(File.DirAssets, "9c.png")
        
    End Select
    Select p2Totalthirddigit
        Case 0
            ivp2totalones.Bitmap=LoadBitmap(File.DirAssets, "0c.png")
        Case 1
            ivp2totalones.Bitmap=LoadBitmap(File.DirAssets, "1c.png")
        Case 2           
            ivp2totalones.Bitmap=LoadBitmap(File.DirAssets, "2c.png")
        Case 3
            ivp2totalones.Bitmap=LoadBitmap(File.DirAssets, "3c.png")
        Case 4
            ivp2totalones.Bitmap=LoadBitmap(File.DirAssets, "4c.png")
        Case 5
            ivp2totalones.Bitmap=LoadBitmap(File.DirAssets, "5c.png")
        Case 6
            ivp2totalones.Bitmap=LoadBitmap(File.DirAssets, "6c.png")
        Case 7
            ivp2totalones.Bitmap=LoadBitmap(File.DirAssets, "7c.png")
        Case 8
            ivp2totalones.Bitmap=LoadBitmap(File.DirAssets, "8c.png")
        Case 9
            ivp2totalones.Bitmap=LoadBitmap(File.DirAssets, "9c.png")
        
    End Select

everything works perfect except when i go negative which is absolutely a probable thing to happen.

I have no idea why it is doing it or what i can do to solve it! any help would be appreciated!!!
 

Ormente

Member
Licensed User
Longtime User
Probably because in this case p2Totalfirstdigit is a string "-" and can not be casted to an integer, so bombing at the first select.
 
Upvote 0

cammel8

Member
Licensed User
Longtime User
Probably because in this case p2Totalfirstdigit is a string "-" and can not be casted to an integer, so bombing at the first select.
Well that makes sense. Normally i dont have to change numbers to a string so i didn't realize you couldn't cast the negative as well.


Can you try this?
B4X:
 p2TotalAsString = (NumberFormat2(Abs(p2Total), 3, 0 ,0, False)))
Ok lemme see if i got this right then?

So basically since i need to know if it is a negative or not i would first pass the variable to see if it is negative. Then get the absolute value for the digits themselves.
B4X:
dim numberisnegative as boolean
 if p1total <0 then
numberisnegative = true
else
numberisnegative=false
end if

Dim p1TotalAsString As String
    p1TotalAsString=p1Total
    p1TotalAsString = (NumberFormat2(Abs(p1TotalAsString, 3, 0 ,0, False)))
    
    Dim p1Totalfirstdigit, p1Totalseconddigit, p1Totalthirddigit As Int
    p1Totalfirstdigit =p1TotalAsString.substring2(0,1)
    p1Totalseconddigit =p1TotalAsString.substring2(1,2)
    p1Totalthirddigit =p1TotalAsString.substring2(2,3)
    
    Select p1Totalfirstdigit
        Case 0
        if numberisneagtive = true then
            ivp1totalhun.Visible=True
            ivp1totalhun.Bitmap=LoadBitmap(File.DirAssets, "negativec.png")
            End If
        Case 1
            ivp1totalhun.Visible=True
            ivp1totalhun.Bitmap=LoadBitmap(File.DirAssets, "1c.png")
        Case 2           
            ivp1totalhun.Visible=True
            ivp1totalhun.Bitmap=LoadBitmap(File.DirAssets, "2c.png")
        Case 3
            ivp1totalhun.Visible=True
            ivp1totalhun.Bitmap=LoadBitmap(File.DirAssets, "3c.png")
        Case 4
            ivp1totalhun.Visible=True
            ivp1totalhun.Bitmap=LoadBitmap(File.DirAssets, "4c.png")
        Case 5
            ivp1totalhun.Visible=True
            ivp1totalhun.Bitmap=LoadBitmap(File.DirAssets, "5c.png")
        Case 6
            ivp1totalhun.Visible=True
            ivp1totalhun.Bitmap=LoadBitmap(File.DirAssets, "6c.png")
        Case 7
            ivp1totalhun.Visible=True
            ivp1totalhun.Bitmap=LoadBitmap(File.DirAssets, "7c.png")
        Case 8
            ivp1totalhun.Visible=True
            ivp1totalhun.Bitmap=LoadBitmap(File.DirAssets, "8c.png")
        Case 9
            ivp1totalhun.Visible=True
            ivp1totalhun.Bitmap=LoadBitmap(File.DirAssets, "9c.png")
        
    End Select

Would that work?

Im not at home to try it out but Im trying to picture it in my head
 
Upvote 0

Ormente

Member
Licensed User
Longtime User
I'm not able to test either (and not very experienced with B4X) but this looks better.
I'm not sure why you test for negativeness in the first case. Doing so you limit your range to -99..999
Maybe you could/should use a simple if test before the select, to check if you have to display a minus or not, then proceed with your selects to display the 3 digits (from the absolute value).

Also, you can replace your first six lines by this:
B4X:
dim numberisnegative as boolean = p1total < 0
Later the code if numberisneagtive = true then can be simplified to: if numberisneagtive then
 
Last edited:
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
There are many ways to do it.

You can test if the number is negative.
You can use Abs()
You can convert to String and remove "-"

I think the important thing is that:

"234".substring2(0,1)

Results "2"

"-234".substring2(0,1)

Results "-"
 
Upvote 0

Claudio Oliveira

Active Member
Licensed User
Longtime User
Looks like you're using p2Totalfirstdigit, p2Totalseconddigit and p2Totalthirddigit only to select which image should be loaded.
If that's the case, this code below might work pretty fine.
You don't really need al those select/case for second and third digit. Just use string concatenation to load the correct PNG file.

Regards

B4X:
Dim p2Totalfirstdigit, p2Totalseconddigit, p2Totalthirddigit As Int
   p2Totalfirstdigit =Abs(p2TotalAsString).substring2(0,1)
   p2Totalseconddigit =Abs(p2TotalAsString).substring2(1,2)
   p2Totalthirddigit =Abs(p2TotalAsString).substring2(2,3)

Select p2Totalfirstdigit
       Case 0
           ivp2totalhun.Visible=False
       Case Else
           ivp2totalhun.Visible=True
           ivp2totalhun.Bitmap=LoadBitmap(File.DirAssets, $"${p2Totalfirstdigit}c.png"$)
End select

ivp2totaltens.Bitmap=LoadBitmap(File.DirAssets, $"${p2TotalSeconddigit}c.png"$)

ivp2totalones.Bitmap=LoadBitmap(File.DirAssets, $"${p2TotalThirddigit}c.png"$)
 
Upvote 0
Top