1 回答

TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個贊
您的數(shù)據(jù)似乎包含帶小 e 的科學(xué)計(jì)數(shù)法。令人煩惱的是,該庫不會攔截,但簡單的替換會對您的情況有所幫助。
String entry = "-7.5212e-06+3.4298e-06i";
ComplexFormat cf = new ComplexFormat();
Complex c = cf.parse(entry.replace('e', 'E'));
System.out.println(c);
//(-0.075212, 0.034298)
但請注意,如果您使用默認(rèn)構(gòu)造函數(shù)(如上所述),您的數(shù)字將被正確解析,但科學(xué)記數(shù)法將不再保留。如果你想保留符號,請使用 DecimalFormat:
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
DecimalFormat formatter = new DecimalFormat("##.####E0", symbols);
ComplexFormat cfWithE = new ComplexFormat(formatter);
Complex c2 = cfWithE.parse(entry.replace('e', 'E'));
System.out.println(c2);
//(-7.5212E-6, 3.4298E-6)
添加回答
舉報