# Rounding A Decimal?

#### PharCyDeD

##### Active Member
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:

#### NJDude

##### Expert
Longtime User
Take a look at NumberFormat and NumberFormat2.

#### nfordbscndrd

##### Well-Known Member
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.

NMG

#### Mahares

##### Expert
Longtime User
Here is an example:
Msgbox(Round2(98.388888767,1) & "%","") yields: 98.4%

Longtime User

#### Azhar

##### Active Member
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

#### klaus

##### Expert
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.
Floor(5.57) = 5
Ceil(5.57) = 6
Floor(-5.57) = -6
Ceil(-5.57) = -5
Best regards.

#### Inman

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

#### Azhar

##### Active Member
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.
Floor(5.57) = 5
Ceil(5.57) = 6
Floor(-5.57) = -6
Ceil(-5.57) = -5
Best regards.

Thanks so much!

Azhar

#### umsid

##### Member
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?

Guenter

#### MLDev

##### Active Member
Longtime User
You can remove the .0 with this:

B4X:
``CronyResult.Text = Round(Floor(Crony_Factor))``

#### umsid

##### Member
Longtime User
Thanks, it works.

#### Justin King

##### New Member
Is it possible to use the Round() function in the designer script?

#### Roger Daley

##### Well-Known Member
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

Replies
1
Views
989
Replies
12
Views
2K
Replies
3
Views
733
Replies
8
Views
2K
Replies
0
Views
1K