on ‎2008 Aug 19 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
Request clarification before answering.
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 ?"
| User | Count |
|---|---|
| 10 | |
| 5 | |
| 4 | |
| 4 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.