我想将BigInteger数字转换为像1.86e + 6这样的小型科学记数法,并再次将该科学记数法转换为Java中的BigInteger数字。请帮忙。
答案 0 :(得分:2)
最简单的方法是使用BigDecimal
来解析或输出科学记数法字符串。
您可以执行以下操作:
BigDecimal bd = new BigDecimal("1.86E+6");
BigInteger bi = bd.toBigInteger();
并反向:
bd = new BigDecimal(bi);
String s = bd.toString();
如果您需要更多用户定义的输出,则可以使用NumberFormatter
。不要忘记将Locale
设置为类似Locale.ROOT
的内容,这样你就不会得到,用逗号作为小数点分隔符(这是我先得到的,在德语中)环境)。例如:
// Create a large, ugly number.
BigInteger bi = BigInteger.valueOf(1001).pow(345);
// Convert to scientific notation using invariant Locale.ROOT
NumberFormat formatter = new DecimalFormat("0.######E0", DecimalFormatSymbols.getInstance(Locale.ROOT));
String str = formatter.format(bi);
System.out.println(bi);
System.out.println();
System.out.println(str);
// No need for a formatter here.
BigDecimal bd = new BigDecimal(str);
BigInteger output = bd.toBigInteger();
System.out.println();
System.out.println(output);
这个输出是:
1411746534642247386926682895653605505594156007645319584856454985824576909030651172402258145880680847831210464829918490010166811079002972726937057623073994129575640372154237767652434547101885830188958723868572869883365738143353635151476880747344348010706072986535185748982964423793694140085891791220972791882178323235649877119554541663599295787824745711388310165587991341807160511741076029768404282877856115942906536866189181255514197337418597936644390730217525723115231014147849887446040444969336884906158293521291748134217314005889949484320602720371789914893639795254884800520873191697159041280591046403928290350948505388703036712226506136642305960716764124836947362932720418554290195995002114233675196543233402547357577387336805972842986766416381431727078044233139876612983206051371851773391882427929601311695575660371227105236375213782469513349953017524299926322617324052803634576283153878896093739315873095260971811967828941219651149370566639839402498088185721432957408746669159107050035686712174548658001777149571278954599340345001
1.411747E1035
1411747000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
(是的,BigIntegers可能很笨重)。
我不知道你的价值有多大,但上面的效果在我的Mac上运行良好。当然,使用只有几位小数的科学记数法会失去很多精确度。