Android Question percentge

sparks9941

Member
Licensed User
How can I display a calculated number in a label as a percentage?
Example: .243687 in my label, i want it to read 24.37%
labelpercent.text = ((label1.text - label2.text ) / label1.text)
i want labelpercent.text to be XX.XX%

in visual basic i used:
labelpercent = Format((label1 - label2) / label1, "percent")
that worked great in vb, i am very new to basic4android, still on trial version
 
Last edited:

JTmartins

Active Member
Licensed User
Someting like

B4X:
labelpercent.Text= "12"& " %"
of course instead of the "12" you should place your variable with the value you need.
 

sparks9941

Member
Licensed User
thanks, but that just added a % sign, i need to convert the decimal to percent with 2 places on each side of decimal
like .2499987 to 25.00%
 

cyiwin

Active Member
Licensed User
you can convert ugly doubles to a more readable string.
B4X:
MyString = NumberFormat2(MyDouble,2,2,2,False)
 

sparks9941

Member
Licensed User
Klaus, that worked pretty good but it comes up with 0.25% and i need it to convert the fraction to % like 25.00%
 

sparks9941

Member
Licensed User
this is what worked well for me in visual basic
lblpercent = Format ((lblratio1 - lblratio2) / lblratio1, "percent") this gave me the actual percent of drop from ratio1 to ratio2 , like 25.89%

so far all i am getting my label to display is 0.25% when i need it to display 25.89% (example)
 

Mahares

Well Known Member
Licensed User
I have a feeling you want something like this example:
B4X:
 Label1.text= 23.8 :Label2.text=18      
labelpercent.Text = NumberFormat2(((Label1.text - Label2.text) / Label1.text *100), 1, 2 ,2 , False) & "%"
'Displays: 24.37%
 

sparks9941

Member
Licensed User
Thank you Mahares, as soon as i seen your post i new it would work, and it does. I should have figured that out but I am learning.
 

wonder

Expert
Licensed User
Thank you for pointing me to that thread, @derez.

To my understanding, NumberFormat2 will be more accurate than Round2 for precise calculations.
Is there any issue however, while using Round2 when the result doesn't have to be that precise?

Most of us are taught that for digits below 5, round down, for 5 and greater round up.
When rounding just one digit, we look at a digital range of 5 cases each - 0,1,2,3,4 and 5,6,7,8,9
I also was taught this way and I guess that it's what everyone uses on it's daily life.
 

derez

Expert
Licensed User
I also was taught this way and I guess that it's what everyone uses on it's daily life.
Yes but checking the mean showed that numberformat is doing the job better.
 

wonder

Expert
Licensed User
Let imagine you're building a Forex Trading app, where a price variation of 1/100th of a cent (depending on the invested value) will result in profits/losses of thousands of dollars.

Would you perform calculations using NumberFormat2 in this app, without hesitation?
 
Last edited:

klaus

Expert
Licensed User
I'm afraid that you are confusing things.
You should use Round if you want to round a numeric value, Round returns a Double !
You should use NumberFormat to display numbers with the given format, NumberFormat returns a String. This has nothing to with calculations !
 

derez

Expert
Licensed User
Would you perform calculations using NumberFormat2 in this app, without hesitation?
You can make the application cheat by more... just know the difference between the two options.
 
Top