How to convert BigDecimal object to a String representation that uses the exponential form? something like: 3.134e67
? I looked into the API and I found toEngineeringString()
but it does not give me what I want.
3 Answers
Have you read the NumberFormat documentation, this is from DecimalFormat: See: http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DecimalFormat.html
Scientific Notation
Numbers in scientific notation are expressed as the product of a mantissa and a power of ten, for example, 1234 can be expressed as 1.234 x 10^3. The mantissa is often in the range 1.0 <= x < 10.0, but it need not be. DecimalFormat can be instructed to format and parse scientific notation only via a pattern; there is currently no factory method that creates a scientific notation format. In a pattern, the exponent character immediately followed by one or more digit characters indicates scientific notation. Example: "0.###E0" formats the number 1234 as "1.234E3".
The number of digit characters after the exponent character gives the minimum exponent digit count. There is no maximum. Negative exponents are formatted using the localized minus sign, not the prefix and suffix from the pattern. This allows patterns such as "0.###E0 m/s".
The minimum and maximum number of integer digits are interpreted together:
If the maximum number of integer digits is greater than their minimum number and greater than 1, it forces the exponent to be a multiple of the maximum number of integer digits, and the minimum number of integer digits to be interpreted as 1. The most common use of this is to generate engineering notation, in which the exponent is a multiple of three, e.g., "##0.#####E0". Using this pattern, the number 12345 formats to "12.345E3", and 123456 formats to "123.456E3".
Otherwise, the minimum number of integer digits is achieved by adjusting the exponent. Example: 0.00123 formatted with "00.###E0" yields "12.3E-4".
The number of significant digits in the mantissa is the sum of the minimum integer and maximum fraction digits, and is unaffected by the maximum integer digits. For example, 12345 formatted with "##0.##E0" is "12.3E3". To show all digits, set the significant digits count to zero. The number of significant digits does not affect parsing.
Exponential patterns may not contain grouping separators.
-
I tried this: `BigDecimal d = new BigDecimal("55"); DecimalFormat df = new DecimalFormat("0.###E0"); System.out.println(df.format(d.toString()));`, but it gives me an error: "Cannot format given Object as a Number" – Eng.Fouad Feb 21 '12 at 00:18
-
It works now, I just replaced `d.toString()` with `d`. Thanks :) – Eng.Fouad Feb 21 '12 at 00:27
Does this help you?
BigDecimal bd = new BigDecimal(3.134e67);
String.valueOf(bd.doubleValue())

- 32,158
- 14
- 82
- 96

- 2,092
- 5
- 33
- 55
Are you looking for something like this?
YY ^ XX (mod QQ)
int fastMod(int YY, int XX, int QQ){
int ZZ; //declare variables
int RR = 1;
while (XX != 0){ //while XX != 0
ZZ = XX % 2; //mod XX by 2
XX= XX/2; //divide XX by 2
if (ZZ == 1) //if ZZ is one
RR = (RR * YY) % QQ; //mod (RR*YY) by QQ
YY= (YY * YY) % QQ; //mod (YY*YY) by QQ
}
return RR; //return int
}
When you start using GIANT numbers you will need to use modular arithmetic. This becomes especially useful when generating large primes like in RSA algorithm. The basics of modular arithmetic covered here: http://www.brainjammer.com/math/modular-arithmetic/

- 18,485
- 28
- 81
- 134
-
I don't get it. How can this helps in converting BigDecimal to the exponential form? – Eng.Fouad Feb 21 '12 at 00:06
-
The point is that you wont be able to work with numbers that large unless you use modular arithmetic. – Dan Kanze Feb 21 '12 at 00:08