基于Java將Excel科學計數法解析成數字
需要注意的是一般的科學表達式是
1.8E12 1.8E-12
而在Excel中的科學表達式是
1.8E+12 1.8E-12
我寫的科學計數法的正則表達式是
(-?d+.?d*)[Ee]{1}[+-]?[0-9]*
導入EXCEL數據時將科學計數法解析成數字,Java代碼:
import java.text.DecimalFormat;import java.util.regex.Pattern;public class Test { static Pattern pattern = Pattern.compile('(-?d+.?d*)[Ee]{1}[+-]?[0-9]*'); static DecimalFormat ds = new DecimalFormat('0'); static boolean isENum(String input) {//判斷輸入字符串是否為科學計數法 return pattern.matcher(input).matches(); } public static void main(String[] args) { String str = '6.18404E+17'; System.out.println(isENum(str)); if (isENum(str)) { String sPhone = ds.format(Double.parseDouble(str)).trim(); System.out.println(sPhone); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: