[英]How to convert String to BigDecimal without scientific notation
[英]How to convert a BigInteger to a scientific notation string and back?
我想將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
類的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上都能正常使用。 當然,僅使用具有十進制數位的科學計數法會損失很多精度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.