Help For Returing a value of a number to the nearest integer.

jothis

Active Member
Licensed User
Hi friends,
in php the ceil() function returns the value of a number rounded UPWARDS to the nearest integer.
B4X:
eg:
echo(ceil(0.60) . "<br />");
echo(ceil(0.40) . "<br />");
echo(ceil(5) . "<br />");
echo(ceil(5.1) . "<br />");
echo(ceil(-5.1) . "<br />");
echo(ceil(-5.9))
this will return as follows
1
1
5
6
-5
-5

Is There Any function to returns the value of a number rounded UPWARDS to the nearest integer in basic4ppc?



Please Help Me
jothis
:sign0085:
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Sorry this is Basic4android code.
Here is the Basic4ppc version:
B4X:
Sub App_Start
    Msgbox(Ceil(0.6))
    Msgbox(Ceil(0.4))
    Msgbox(Ceil(5))
    Msgbox(Ceil(5.1))
    Msgbox(Ceil(-5.1))
    Msgbox(Ceil(-5.9))
    Msgbox(Ceil(-6))
End Sub
Sub Ceil(Num As double) As double
    Dim f As Double
    f = Int(Num)
    If f = Num Then Return f Else Return f + 1
End Sub
 

jothis

Active Member
Licensed User
Thank you

Thank you Erel and klaus

Thankyou For Helping Me

I Also Found The Round() Function in b4ppc

Msgbox(Round(-55.55))

Returned as -56

:sign0060:
 
Last edited:

jothis

Active Member
Licensed User
msgbox(round(-55.49)) returns -55 though.
Round(), rounds up or down.

Thank you taximania

But this code shows some error
B4X:
Sub App_Start
    Msgbox(Ceil(0.6))
    Msgbox(Ceil(0.4))
    Msgbox(Ceil(5))
    Msgbox(Ceil(5.1))
    Msgbox(Ceil(-5.1))
    Msgbox(Ceil(-5.9))
    Msgbox(Ceil(-6))
End Sub
Sub Ceil(Num As double) As double
    Dim f As Double
    f = Int(Num)
    If f < Num Then Return f Else Return f + 1
End Sub

Error Like
Sub Ceil(Num As double) As double
Syntax error
jothis
 
Last edited:

taximania

Well-Known Member
Licensed User
Longtime User
Try

Sub App_Start
Msgbox(Ceil(0.6))
Msgbox(Ceil(0.4))
Msgbox(Ceil(5))
Msgbox(Ceil(5.1))
Msgbox(Ceil(-5.1))
Msgbox(Ceil(-5.9))
Msgbox(Ceil(-6))
End Sub
Sub Ceil(num)
f = Int(Num)
If f = Num Then Return f Else Return f + 1
End Sub

I'm using Basic4ppc Ver 6.8
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…