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

Problem java function

Former Member
0 Likes
459

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

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;