on 08-26-2015 7:01 PM
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
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);
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.