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

Giri Dhar
  • 149
  • 2
  • 12

1 Answers1

1

try this

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

to format use

BigDecimal.toEngineeringString()
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • 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?? – Giri Dhar Jan 21 '14 at 04:54
  • You cannot case String to BigDecimal you can parse it, new BigDecimal("6.61409E1283") parses it – Evgeniy Dorofeev Jan 21 '14 at 04:58
  • 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...... – Giri Dhar Jan 21 '14 at 05:06
  • @GiriDhar doing exactly that will result in loss of precision. – Obicere Jan 21 '14 at 05:07