gpt4 book ai didi

java - 如何将 BigInteger 转换为科学计数法字符串并返回?

转载 作者:行者123 更新时间:2023-11-30 02:51:25 24 4
gpt4 key购买 nike

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

最佳答案

最简单的方法是使用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 上运行良好。当然,使用只有几位小数的科学记数法会损失很多精度。

关于java - 如何将 BigInteger 转换为科学计数法字符串并返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38534460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com