cancel
Showing results for 
Search instead for 
Did you mean: 

Converting Float to Int

Former Member
0 Kudos

HI,

How could I convert float value to Int and pass ? I am using UDF ..

Eg:

11:9999999 I want to pass 12, Its a run time Program so dont know how the value would be...

Is there any function to round it off?

Thanks

Rajeev

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Rajeev,

Try to use Math.rint().

float f = new Float(input).floatValue();

int i = (int)Math.rint(f);

return String.valueOf(i);

Regards,

Saptak.

Answers (6)

Answers (6)

smavachee
Active Contributor
0 Kudos

Hi Rajiv,

For converting the float into an integer, simply applied the type casting process and get the integer value.

public class UDF

{

static float f = 90.0F;

public static void main(String arg[])

{

int i = (int)f;

}

}

Thanks

Sunil M

Former Member
0 Kudos

Hi Rajeev,

In your scenario there is no need to use UDF,there is CEIL an arithmatic standard function which u can use for this.

function of ceil is as the name conveys to ceil the number to nearest integer.

i.e; if the num is 7.89 ceil( 7.87) is 8 and ceil (-7.89) is 6.

regards,

prasad.k

Former Member
0 Kudos

just assaign 11.9999999 to int variable

int value =11.9999999;

then value will be equal to 12

if you want to return this int value from a UDF then assaign value to String variable as

String res=value+"";

return res;

Former Member
0 Kudos

Hi Guys,

I am unable to convert float to int.. I tried wtih all the mentioned syntax but there is some error in UDF..

UDF will give result as a value or null so I need to convert with in the code itself..

How can I convert 11.99999 to 12

there aer 4 values which needs to be divided and the result to be passed..

Is there any round function so that I can convert this value to nearest highest value with in the UDF?

Any inputs

Rajeev

Edited by: rajeev raj on Sep 17, 2009 5:06 PM

Former Member
0 Kudos

Hi Rajeev,

We have a standard ceil function in airthmetic functions. You can use that for your current issue. You have also round function in airhtmetic, if you want that you can use it. No udf is required for this.

Regards,

---Satish

Former Member
0 Kudos

Hi Rajeev,

If your issue doesnot solve with standard functions then you can write this udf. Create a value udf with one input argument 'a'.

Imports: java.*;

// Add this code:

float f = Float.valueOf(a.trim()).floatValue(); //Udf will only take string as input this line will convert the input as into float value

int b;

b = (int) f; //convert float to int in this line

String s = String.valueOf(b); //converting the int to string for output

return s; //returning back the ouput.

So if input is 11.9999 the output is 12.

Regards,

---Satish

Former Member
0 Kudos

Thank You ALL,

I got it resolved by just adding the following line in my UDF

int i = (int)Math.rint(my float variable);

Thanks Saptak, Points awarded.

Thanks once again.

rajasekhar_reddy14
Active Contributor
0 Kudos

Hi,

use below statemnts to convert float to int.

float f = 11.44;

int i = (int)(f + 0.5f);

i value after conversion 12.

Regards,

Raj

former_member187339
Active Contributor
0 Kudos

Hi,

Try this udf,

input is the input for this simple udf


Float FNum = new Float(input);
float f = FNum.floatValue();
int i = (int)f;

Integer INum = new Integer(i);
return INum.toString();

Regards

Suraj

Former Member
0 Kudos

Hi,

if you have a float value in a UDF you could use the intValue() method.

Example:

Float f = new Float("4.5");

int i = f.intValue();

the value of i will be 4.

Regards

Patrick