How to show decimal places in a calculated answer

Ksmith1192

Member
Licensed User
Longtime User
Hi Guys!

So I have been creating this calculator, that is composed of a label in the middle with changing values, and increase/decrease buttons on the sides. I have it working to where I click and hold the button, it goes up by 1. How can I make it go up by .01 and show it?

Here is my calculation code
B4X:
Sub Timer1_Tick
    Dim price,Pounds As Int 
    price = PriceLabel.Text 
    If PriceButtonDown Then 
        If PriceButtonPressed ="Increase" Then 
            price = price + 1
        Else
         If price>0 Then
               price = price - 1
         End If
        End If
        PriceLabel.Text = price
    End If
   
   Pounds = PoundsLabel.Text 
    If PoundsButtonDown Then 
        If PoundsButtonPressed ="Increase" Then 
            Pounds = Pounds + 1
        Else
         If Pounds>0 Then
               Pounds = Pounds - 1
         End If
        End If
        PoundsLabel.Text = Pounds
    End If
   
End Sub
 

derez

Expert
Licensed User
Longtime User
Your price and pounds are declared as int, so they only show integers.
If you declare them as float or double, you can use price = price + 0.01
To have the display show exactly two digits after the point use numberformat(price,0,2)
 
Upvote 0

Ksmith1192

Member
Licensed User
Longtime User
Thank you!

Thanks! It definitely worked well, but when i hold it down long enough it starts doing this (picture attached.) Any way to restrict it to 2 decimal places without having thousands of numbers pop up?
 

Attachments

  • Capture.PNG
    37.4 KB · Views: 364
Upvote 0

Ksmith1192

Member
Licensed User
Longtime User
Yeah, I have tried that and it still is showing up with around 8 decimal places. I have attached the sample program, if you want to check that out. Here is the code for the calculation.
B4X:
Sub Timer1_Tick
    Dim price,Pounds As Float
    price = PriceLabel.Text 
    If PriceButtonDown Then 
        If PriceButtonPressed ="Increase" Then 
            price = price + .01
        Else
         If price>0 Then
               price = price - .01
         End If
        End If
      NumberFormat(price,0,2)
        PriceLabel.Text = price
    End If
   
   Pounds = PoundsLabel.Text 
   NumberFormat(Pounds,0,2)
    If PoundsButtonDown Then 
        If PoundsButtonPressed ="Increase" Then 
            Pounds = Pounds + .01
        Else
         If Pounds>0 Then
               Pounds = Pounds - .01
         End If
        End If
      NumberFormat(Pounds,0,2)
        PoundsLabel.Text = Pounds
    End If
   
End Sub
 

Attachments

  • Increase_Decrease.zip
    180.8 KB · Views: 312
Upvote 0

klaus

Expert
Licensed User
Longtime User
When you use a new function you should look at the help to know how it works.

Here is the code:
B4X:
Sub Timer1_Tick
    Dim price,Pounds As Float
    price = PriceLabel.Text 
    If PriceButtonDown Then 
        If PriceButtonPressed ="Increase" Then 
            price = price + .01
        Else
            If price>0 Then
                price = price - .01
            End If
        End If     
        PriceLabel.Text = NumberFormat(price,0,2)
    End If
    
    Pounds = PoundsLabel.Text 
    If PoundsButtonDown Then 
        If PoundsButtonPressed ="Increase" Then 
            Pounds = Pounds + .01
        Else
            If Pounds>0 Then
                Pounds = Pounds - .01
            End If
        End If
        PoundsLabel.Text = NumberFormat(Pounds,0,2)
    End If
    
End Sub
I suggest you to download the B4AHelp tool it's a MUST HAVE.
I have it always running.

Best regards.
 
Upvote 0

Ksmith1192

Member
Licensed User
Longtime User
Klaus, (or anyone for that matter)

I tried to do what you explained earlier, and add a new field to display a result,and tried to research it, but i kept coming up dry.

I need this number to be 2 decimal places as well, but it keeps adding around 8 decimal places after.

B4X:
Sub Timer1_Tick
    Dim price,pounds,results As Float
    price = PriceLabel.Text 
    If PriceButtonDown Then 
        If PriceButtonPressed ="Increase" Then 
            price = price + .05
        Else
         If price>0 Then
               price = price - .05
         End If
        End If
        PriceLabel.Text = NumberFormat(price,0,2)
    End If
   
   pounds = PoundsLabel.Text 
   NumberFormat(pounds,0,2)
    If PoundsButtonDown Then 
        If PoundsButtonPressed ="Increase" Then 
            pounds = pounds + .05
        Else
         If pounds>0 Then
               pounds = pounds - .05
         End If
        End If
      PoundsLabel.Text = NumberFormat(pounds,0,2)
    End If
   ResultsLabel.Text = ((PriceLabel.Text / 50) * PoundsLabel.Text)
   ResultsLabel.Text = NumberFormat(results,0,2) 
   
End Sub

And is there a way to add for the results to say "$(Results) per day' into the output?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You are mixing up numbers and strings !
Calculations are made with numbers.
Display is done with strings.
It's not good practice to calculate with string fields (at least for me), eventhough it does work.
You can transform strings to numbers like
price = PriceLabel.Text
and numbers to strings like
PriceLabel.Text = price not formatted
or
PriceLabel.Text = NumberFormat(price,0,2) formatted.

I'd suggest you define the variables Price, Pounds and Results in Globals or even Process_Globals and the Timer1_Tick routine like this:
B4X:
Sub Timer1_Tick
    If PriceButtonDown Then 
        If PriceButtonPressed ="Increase" Then 
            Price = Price + .05
        Else
            If Price>0 Then
                Price = Price - .05
            End If
        End If
        PriceLabel.Text = NumberFormat(Price,0,2)
    Else If PoundsButtonDown Then 
        If PoundsButtonPressed ="Increase" Then 
            Pounds = Pounds + .05
        Else
            If Pounds>0 Then
                Pounds = Pounds - .05
            End If
        End If
        PoundsLabel.Text = NumberFormat(Pounds,0,2)
    End If
    Results = Price / 50 * Pounds
    ResultsLabel.Text = "$ " & NumberFormat(Results,0,2) & " per day"
End Sub
You must initialize the Price, Pounds and Results variable values somewhere in the code.

Best regards.
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Here is what you need. I commented the 2 bad lines you have
B4X:
results = ((PriceLabel.Text / 50) * PoundsLabel.Text)
         ResultsLabel.Text = NumberFormat(results,0,2)
         ResultsLabel.Text="$" & ResultsLabel.Text & " per day"
'    ResultsLabel.Text = ((PriceLabel.Text / 50) * PoundsLabel.Text)
'    ResultsLabel.Text = NumberFormat(results,0,2)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…