on 08-19-2008 5:11 PM
Hello guys,
I've done a java function to handle the fact that i move the decimal of a number to 2 instead of 3, 4, 5 or else.
For exemple, if i have in input the field 159, 7934 then i should have in output 159,79.
My problem is that it rounds the number !! So i have in output 159,8 !
Here's my function :
// Round the given input value to given number of decimals
// The result will be a floating point number (not integer value)
String zeroes = "00000000000000";
String numDec = numDecimals;
// To prevent a Divide by zero error default to 1 which would lead to a No-op
if (numDec.equals("") || numDec.equals("0"))
numDec = "0";
String strFactor = "1" + zeroes.substring(0, Integer.parseInt(numDec));
int intFactor = Integer.parseInt(strFactor);
float i =Float.parseFloat(inputValue);
i = Math.round(i*intFactor);
i = i/intFactor;
return Float.toString(i);
Like you see, i'm putting in string format for the output because i am using a replacestring after in order to suppress the '.'
I think the problem come from that.
The solution would be to suppress the '.' in my UDF but i don't know how to code it in java.
Is someone knows please ?
Thanks by advance,
JP
Thanks for the answer.
I am not a java expert so i didn't understand everything you wrote 😕
So, what i've done, it's to create a second UDF with the function nf you put.
Nevertheless, it seems it doesn't know the "nf" ! Problem of API ?
How this function will bring back my 100.8 to 100.79 for example ?
I know that my problem is the math.round but i don't know how to do it whitout using it...
@Emit : Why do you put the number in str ? Isn't the field in input i should put instead ?
thanks,
JP
Edited by: PAIN Jean-Philippe on Aug 19, 2008 7:08 PM
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ok my UDF is now :
// Round the given input value to given number of decimals
// The result will be a floating point number (not integer value)
String zeroes = "00000000000000";
String numDec = numDecimals;
// To prevent a Divide by zero error default to 1 which would lead to a No-op
if (numDec.equals("") || numDec.equals("0"))
numDec = "0";
//String strFactor = "1" + zeroes.substring(0, Integer.parseInt(numDec));
//int intFactor = Integer.parseInt(strFactor);
double i =Double.parseDouble(inputValue);
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
String n = nf.format(i);
return n ;
BUT, when i put 2 digits, it still "rounding" my number even if i use a double or a float !
Nevertheless, when i want 3 digits, it still "rounding" !
Is it impossible to avoid the "round machine ?"
Hello,
You can keep you existing code and let it calculate to 100.8. After that use the following code
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
String n = nf.format(100.8);
If you want more details, you can refer the NumberFormat in the Java documentation.
Let me know if it works or not.
Regards,
Akshay
Edited by: Akshay Salunke on Aug 19, 2008 1:25 PM
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
You can do the following way if you don't want to round off the number. This is sample code which will work, however you need to take care of the condition , if there is only one digit after decimal you need to handle that case as well inside if condition. Hope it solves your query
String retStr = "";
String str = "159.7934";
int index = str.indexOf(".");
if(index!=-1)
{
retStr=str.substring(0,index)+str.substring(index,index+3);
}
else
{
retStr =str;
}
return retStr;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
90 | |
10 | |
10 | |
10 | |
7 | |
7 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.