on 09-17-2009 2:06 PM
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.