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

Problem java function

Former Member
0 Likes
464

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

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

Former Member
0 Likes

Hello,

OK, now use this code

float i =Float.parseFloat(<input value>);

NumberFormat nf = NumberFormat.getNumberInstance();

nf.setMaximumFractionDigits(2);

String n = nf.format(i);

Regards,

Akshay

Former Member
0 Likes

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 ?"

Santhosh_Vellingiri
Active Contributor
0 Likes

Hi,

This works..!

imports: java.math.BigDecimal;

public String test(String a,Container container)
{
BigDecimal bd = new BigDecimal(*a*);
return bd.setScale(*2*, BigDecimal.ROUND_DOWN).toString();
}

a is the input to the UDF.

2 is the no of digits.

Thanks

SaNv...

Former Member
0 Likes

Yes it works !

thank you very much all for your help !!

JP