0

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....

0

1 Answer 1

1

try this

BigDecimal temp_val1 = new BigDecimal("6.61409E1283");

to format use

BigDecimal.toEngineeringString()
Sign up to request clarification or add additional context in comments.

4 Comments

BigDecimal temp_val1 = new BigDecimal("6.61409E1283"); gives 6.61409E+1283. BigDecimal.toEngineeringString() returns the string representation of the decimal, how to cast this string to bigdecimal??
You cannot case String to BigDecimal you can parse it, new BigDecimal("6.61409E1283") parses it
BigDecimal temp_val1 = factorial(555); gives 66140856092779467090983316712427699021235xxxxxxxxxxxxxxxxxxxxx. Now i will format using DecimalFormat to show in scientific notation. NumberFormat sci_formate = new DecimalFormat("0.#####E0");String temp_s1 = sci_formate.format(temp_val1); which gives 6.61409E1283. Now i need to convert this String(6.61409E1283) back to BigDecimal which gives same value as temp_val1......
@GiriDhar doing exactly that will result in loss of precision.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.