-3

I would like to check whether the double value has exponent or not. There is no such method in Math using which I can determine. I do not want to convert it to string and then use indexOf() or contains() methods.

Thanks in advance.

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
Technocrat
  • 15
  • 1
  • 1
  • Could you give us an example of double with exponent, please? – luanjot Oct 17 '13 at 08:35
  • 2
    have you tried to do something? – X-Pippes Oct 17 '13 at 08:35
  • 2
    Could you give us an example of a double without an exponent? Last time I checked the IEEE spec, every double had an exponent .... – Ingo Oct 17 '13 at 08:36
  • `if(isExponent(num)) return true` - Now you just need to implement `isExponent`. – Maroun Oct 17 '13 at 08:36
  • @X-Pippes Op has already tried and that's why he/she is telling that there is no such method in Math and he does not want to use contains or indexof method string class – SpringLearner Oct 17 '13 at 08:36
  • @luanjot For eg. I have Double dbl = 9.7561E-4; For this I need to check which has got exponent. – Technocrat Oct 17 '13 at 08:45
  • @user2889593 you don't need to check: it has an exponent. – Ingo Oct 17 '13 at 08:51
  • @user2889593 check this [link](http://stackoverflow.com/questions/5495891/how-to-check-exponent-of-a-number-in-java) – SpringLearner Oct 17 '13 at 08:57
  • Once I come to know the value (double type) has got any exponent while any arithmetic operation. I am able to convert into the desired format. My only question was is there any way to determine the new value has exponent without not converting to string then using contains ("E") || contains ("e"). – Technocrat Oct 17 '13 at 12:03

1 Answers1

4

Internally all doubles are represented the same way. (They all have binary exponents, though you don't see those.)

Whether they are printed with an exponent or not is only a formatting issue. It is not meaningful to test whether the double itself has an exponent. The right way to test whether it is formatted with an exponent it is to take the string representation and use str.contains("E") || str.contains("e").

You can use a format string or DecimalFormat object to get some control over how the double is converted to a String. E.g., String.format("%f", 9.7561E-4) (number with exponent) returns the string 0.000976 (no exponent).

Boann
  • 48,794
  • 16
  • 117
  • 146
  • OP has mentioned in the question that he/she does not want to use String.conatins() – SpringLearner Oct 17 '13 at 08:47
  • 3
    @javaBeginner Yes, but this is a non-sensical requirement of OP. OP must learn the difference between external and internal representation of data, therefore +1. – Ingo Oct 17 '13 at 08:48