I have a string like: "2E6 3.34e-5 3 4.6"
and I want to use replaceAll to replace tokens like:
"((\\-)?[0-9]+(\\.([0-9])+)?)(E|e)((\\-)?[0-9]+(\\.([0-9])+)?)"
(i.e. two numbers with e or E between them) into the equivalent normal number format (i.e. replace "2E6"
with "2000000"
and "3.34e-5"
with "0.0000334"
)
I wrote:
value.replaceAll("((\\-)?[0-9]+(\\.([0-9])+)?)(E|e)((\\-)?[0-9]+(\\.([0-9])+)?)", "($1)*10^($6)");
but I would like to actually multiply the 1st argument by 10 to the power of the 2nd argument, not just writing it that way .. Any ideas?
UPDATE
I did the following based on your suggesions:
Pattern p = Pattern.compile("((\\-)?[0-9]+(\\.([0-9])+)?)(E|e)((\\-)?[0-9]+(\\.([0-9])+)?)");
Matcher m = p.matcher("2E6 3.34e-5 3 4.6");
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "WHAT HERE??"); // What HERE ??
}
m.appendTail(sb);
System.out.println(sb.toString());
UPDATE
Finally, this is what I reached:
// 32 #'s because this is the highest precision I need in my application
private static NumberFormat formatter = new DecimalFormat("#.################################");
private static String fix(String values) {
String[] values_array = values.split(" ");
StringBuilder result = new StringBuilder();
for(String value:values_array){
try{
result.append(formatter.format(new Double(value))).append(" ");
}catch(NumberFormatException e){ //If not a valid double, copy it as is
result.append(value).append(" ");
}
}
return result.toString().substring(0, result.toString().length()-1);
}