Format BigDecimal to string with scientific notation and viceversa - java

I have a function called factorial which calculates factorial for a large number and returns in BidDecimal
BigDecimal temp_val1 = factorial(555);
the above gives 66140856092779467090983316712427699021235xxxxxxxxxxxxxxxxxxxxx
Now i have to format the BigDecimal value to string with scientific notation
NumberFormat sci_formate = new DecimalFormat("0.#####E0");
String temp_s1 = sci_formate.format(temp_val1);
the above gives 6.61409E1283
Now i need to convert the string temp_s1(6.61409E1283) back to BigDecimal which gives the value of temp_val1....???? How to do that....

try this
BigDecimal temp_val1 = new BigDecimal("6.61409E1283");
to format use
BigDecimal.toEngineeringString()

Related

Conversion of String to BigDecimal format

I have a String which is like "$1,234.00". I need to convert it into a BigDecimal value. Cannot do it utilising normal BigDecimal conversion methods as it throws NumberFormatException. Is there a way it can be achieved?
"$1,234.00" is a formatted numeric text, so you need to parse the number using a NumberFormat.
In particular, you need a DecimalFormat so you can call the setParseBigDecimal(true) method, since you want a BigDecimal as the result, otherwise it would likely have returned a Double.
DecimalFormat format = new DecimalFormat("ยค#,##0.00", DecimalFormatSymbols.getInstance(Locale.US));
format.setParseBigDecimal(true);
BigDecimal number = (BigDecimal) format.parse(input);

How to convert BigInteger to scientific notation [duplicate]

I want to convert BigInteger number into a small scientific notation like 1.86e+6 and again reconvert that scientific notation into BigInteger number in Java. Please help.
The easiest way is to use a BigDecimal to parse or output the scientific notation string.
You can can do something like:
BigDecimal bd = new BigDecimal("1.86E+6");
BigInteger bi = bd.toBigInteger();
and reverse:
bd = new BigDecimal(bi);
String s = bd.toString();
Update
If you need more user-defined output, then you can use NumberFormatter. Don't forget to set the Locale to something like Locale.ROOT, so you won't get, say a comma as the decimal separator (which is what I got first, in the German locale). Example:
// 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);
The output of this is:
1411746534642247386926682895653605505594156007645319584856454985824576909030651172402258145880680847831210464829918490010166811079002972726937057623073994129575640372154237767652434547101885830188958723868572869883365738143353635151476880747344348010706072986535185748982964423793694140085891791220972791882178323235649877119554541663599295787824745711388310165587991341807160511741076029768404282877856115942906536866189181255514197337418597936644390730217525723115231014147849887446040444969336884906158293521291748134217314005889949484320602720371789914893639795254884800520873191697159041280591046403928290350948505388703036712226506136642305960716764124836947362932720418554290195995002114233675196543233402547357577387336805972842986766416381431727078044233139876612983206051371851773391882427929601311695575660371227105236375213782469513349953017524299926322617324052803634576283153878896093739315873095260971811967828941219651149370566639839402498088185721432957408746669159107050035686712174548658001777149571278954599340345001
1.411747E1035
1411747000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
(Yes, BigIntegers can be unwieldy).
I don't know how big your values are, but the above works well on my Mac. Of course, using scientific notation with only a few decimal digits will lose a lot of precision.

BigDecimal values wants set a new scale

I have a BigDecimal value, for example
BigDecimal bdVal = new BigDecimal("3.141592653");
I really want this value printed to be
dbVal: 3.141600000
What should I do for that ???
If you would like to round the value up to four decimal places, use
BigDecimal rounded = bdVal.setScale(4, RoundingMode.HALF_UP);
To print it with nine zeros, use this format:
DecimalFormat decFormat = new DecimalFormat("0.000000000");
String formatted = decFormat.format(rounded);
Demo.

Parse double on string giving wrong result

In my project I am getting a string values from an api and need to pass double values to another api. When I try to parse from string values to double I am not getting the original data.
Here is the code.
String l1="11352721345377306";
String l2="11352721346734307";
String l3="11352721346734308";
String l4="11352721346734309";
DecimalFormat df = new DecimalFormat(".00");
System.out.println(df.format(Double.parseDouble(l1)));
System.out.println(df.format(Double.parseDouble(l2)));
System.out.println(df.format(Double.parseDouble(l3)));
System.out.println(df.format(Double.parseDouble(l4)));
The output is
11352721345377306.00
11352721346734308.00
11352721346734308.00
11352721346734308.00
What went wrong? Is there any problem with parsing? How can i get the original values back.?
Edit: Without using Decimal Format:
1.1352721345377306E16
1.1352721346734308E16
1.1352721346734308E16
1.1352721346734308E16
You can't get original values back. Refer this Java's Floating-Point (Im)Precision.
double only has 15/16 digits of accuracy and when you give it a number it can't represent, it takes the closest representable number.
What is the problem ? ".00" ? If you don't need this, why using a Double ?
You can try like this...
String l1="11352721345377306";
String l2="11352721346734307";
String l3="11352721346734308";
String l4="11352721346734309";
Double d1 = Double.parseDouble(l1);
Double d2 = Double.parseDouble(l2);
Double d3 = Double.parseDouble(l3);
Double d4 = Double.parseDouble(l4);
System.out.println(d1.longValue());
System.out.println(d2.longValue());
System.out.println(d3.longValue());
System.out.println(d4.longValue());
Edit, with BigDecimal to get the correct values:
String l1="11352721345377306";
String l2="11352721346734307";
String l3="11352721346734308";
String l4="11352721346734309";
BigDecimal bd1 = new BigDecimal(l1);
BigDecimal bd2 = new BigDecimal(l2);
BigDecimal bd3 = new BigDecimal(l3);
BigDecimal bd4 = new BigDecimal(l4);
System.out.println(bd1);
System.out.println(bd2);
System.out.println(bd3);
System.out.println(bd4);
Output is:
11352721345377306
11352721346734307
11352721346734308
11352721346734309
You can use
double d = Math.round(Double.parse(yourString) * 100.0) / 100.0;
to get double with rounded decimals.
For printing use:
String formatted = String.format("%.2f", yourDouble);

String 999999999.9999999999 is rounded to 1,000,000,000 BigDecimal

I have a problem where my 999999999.9999999999 String is being rounded to 1,000,000,000 BigDecimal when I parse it.
Code:
NumberFormat format = new DecimalFormat("#.0000000000######");
format.setParseIntegerOnly(false);
Number number = format.parse(text);
Result:
number = 1.0E9
If I use the same format to parse the result BigDecimal back to String:
format.format(number) results in 1,000,000,000
Why does this happen and how I can force the original format.parse(text) call not to round?
Unless there's some code you've omitted, parse is returning a Double, not a BigDecimal. (Try System.out.println(number.getClass()) to check this.)
If you want a BigDecimal, you have to use setParseBigDecimal to tell it to return a BigDecimal. (And to do that, format has to be declared as DecimalFormat.)
DecimalFormat format = new DecimalFormat("#.0000000000######");
format.setParseBigDecimal(true);
Number number = format.parse(text);

Categories

Resources