cancel
Showing results for 
Search instead for 
Did you mean: 

Udf for chceking the char from input and doing power of function

former_member452321
Participant
0 Kudos

Hi I am getting the input value amount  3.98595E7 or 3.0004E4 and I need to check if E present in the input and get the number after E and use power maths function I am not getting always E. if there is no E I need straight map to target Thanks

Accepted Solutions (1)

Accepted Solutions (1)

vinaymittal
Contributor
0 Kudos

use this

import this

import java.text.*

***************************************************************************************************

String str = "3.98595E7";   // you can use here String str = field[0]

  

  double d =0.0;

  if(str.indexOf("E")!=-1)

  {

  d = Double.parseDouble(str.substring(0,str.indexOf("E")));

  int power = Integer.parseInt(str.substring(str.indexOf("E")+1, str.length()));

  int num = (int)Math.pow(10,power);

        NumberFormat formatter = new DecimalFormat();

        formatter.setMaximumFractionDigits(25);

  String result = formatter.format(d*num);

  result = result.replace(",","");

     return result;

}

else

return str;

//return this result;

*************************************************************************************************

original code below

import java.text.*;

public class HelloWorld {

  public static void main(String[] args) {

   /* package whatever; // don't place package name! */

  String str = "3.98595E7";

  

  double d =0.0;

  if(str.indexOf("E")!=-1)

  {

  d = Double.parseDouble(str.substring(0,str.indexOf("E")));

  int power = Integer.parseInt(str.substring(str.indexOf("E")+1, str.length()));

  System.out.println(d + " " + power);

  int num = (int)Math.pow(10,power);

  NumberFormat formatter = new DecimalFormat();

        formatter.setMaximumFractionDigits(25);

  String result = formatter.format(d*num);

  result = result.replace(",","");

  System.out.println("result ="+result);

  }

  }

}

Answers (1)

Answers (1)

former_member184720
Active Contributor
0 Kudos

try this

if(var1.indexOf("E") >0)

return Double.toString(Math.pow(Double.parseDouble(var1.substring(0,var1.indexOf("E"))), Double.parseDouble(var1.substring(var1.indexOf("E")+1,var1.length()))));

else

return var1;

-------------

if you do not want to handle everything in UDF, validate the input string to see if it contains "E" using text function "IndexOf".

If it is greater than zero then pass it to UDF which will return the value after "E" which can be mapped to arithmetic function Power

UDF code"

return var1.substring(var1.indexOf("E")+1,var1.length());

if not just pass it directly.

use if then else function for this.

former_member452321
Participant
0 Kudos

Thank you. How should I use of power of 10 in your udf input >if E present then I should multiply the input with power of 10 else straight map

former_member184720
Active Contributor
0 Kudos

Your question is not clear. Please elaborate with some sample cases.

You just need to pass the input to udf, it checks if it contains "E" or not.

if it contains E the it get's the number after "E" and applies the power function on the input before "E"

300E3 -> power(300,3)

100 -> 100 output

4.00E4 -> power(4.00,4)

former_member452321
Participant
0 Kudos

if the input value is 2.7866205E7 then it means 2.7866205 multiplied by power of 10 ie  2.7866205 X 10,000,000  we need to multiply with 10 with number of times after E else straight map to target without any calculation

former_member452321
Participant
0 Kudos

I am expecting like this return ( (var1.substring(0,var1.indexOf("E")))*(10,(Math.pow(var1.indexOf("E")+1,var1.length())))) but getting some errors

markangelo_dihiansan
Active Contributor
0 Kudos

Hi Mahesh,

You can use this UDF:

Testing:

Regards,

Mark

former_member452321
Participant
0 Kudos

Thank you . I am getting this error parseDouble(java.lang.String) in java.lang.Double cannot be applied to (int) double mult = Math.pow(10,Double.parseDouble(var1.indexOf("E")+1));