Rounding A Decimal?

PharCyDeD

Active Member
Licensed User
Longtime User
How do you go about rounding a decimal in B4A? I have a percent that is being calculated in my app and I just want it to be like: xx.x% instead of something crazy like 98.388888767. I do want to keep the first number after the decimal so the example would become 98.4%


:sign0163:
 

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
How do you go about rounding a decimal in B4A? I have a percent that is being calculated in my app and I just want it to be like: xx.x% instead of something crazy like 98.388888767. I do want to keep the first number after the decimal so the example would become 98.4%

Round2 (Number As Double, DecimalPlaces As Int) As Double

Rounds the given number and leaves up to the specified number of fractional digits.
 
  • Like
Reactions: NMG
Upvote 0

Azhar

Active Member
Licensed User
Longtime User
Old VB6 Int(nn.nn) and B4A Round functions

Hi

I can't seem to find a function in core for the old VB6 Int() function.

I.e if I have a double say 5.57 the old VB6 Int(5.57) will yield 5
Both B4A Round functions will yield 6 or 6.nn depending on whether you used Round2 with n number of decimal places etc.

Is there a similar function to just truncate off all the decimal places and take just the whole number?

Thanks
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Yes, Floor(x) is equivalent to VB6 Int(x).
In Java Int is the keyword for Integer.
You have also a Ceil(x) function.
With your example
Floor(5.57) = 5
Ceil(5.57) = 6
Floor(-5.57) = -6
Ceil(-5.57) = -5
Best regards.
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
I believe to get the integer part alone, you simply need to equate the double variable to Int variable
 
Upvote 0

umsid

Member
Licensed User
Longtime User
I have some Problems with the Floor function:

I have this line in my source:

CronyResult.Text =Floor(Crony_Factor)
the Crony_Factor is: 123.8467365237

the output in the CronyResult Flied is 123.0
I do not know, how to delete the .0 ???

is it a problem of the Floor
or of the convert to text
or of the output?


Thanks for a short reply
Guenter
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
PharCyDeD

Just for clarity, "rounding" has been a debate for some time. Have a look at the thread linked below.
The simple answer is, for rounding numbers NumberFormat2 is the correct solution. Erel has been hammering this for sometime even though it took me sometime to catch on he is of course correct.
Round2 is not "technically" correct although for most common uses is close enough.

For obtaining the integer part only Floor and Ceiling are the way to go.


Round2 Vs NumberFormat2

Regards Roger
 
Upvote 0
Top