小编典典

如何将BigInteger转换为科学记数字符串并返回?

java

我想将BigInteger数转换为小的科学计数法,例如1.86e + 6,然后再次将该科学计数法转换为Java中的BigInteger数。请帮忙。


阅读 436

收藏
2020-11-30

共1个答案

小编典典

最简单的方法是使用a 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上都能正常使用。当然,只用十进制数字表示的科学计数法会损失很多精度。

2020-11-30