cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Problem java function

Former Member
0 Likes
465

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

View Entire Topic
Former Member
0 Likes

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