0
  • Can any number be converted into a Double in Java ?

  • If not, is there a Number class that could parse any numerical type and that would provide basic mathematical operations ?

I have numbers that can be either int, double, float, long, unsigned float or unsigned long. If I could just call them "Number" it will be easier as all I need from them is to operation like equals, greater than, ...

Julien
  • 2,616
  • 1
  • 30
  • 43
  • 2
    https://docs.oracle.com/javase/8/docs/api/java/lang/Number.html – maio290 May 27 '20 at 14:28
  • Are you looking for a numerical type that provides both arbitrary precision and unlimited size? – Thomas Jager May 27 '20 at 14:28
  • are you trying to write a generic class or something – Lazycoder-007 May 27 '20 at 14:37
  • @ThomasJager I don't know. I have numbers that can be either int, double, float, long, unsigned float or unsigned long. If I could just call them "Number" it will be easier as all I need from them is to operation like equals, greater than, ... – Julien May 27 '20 at 14:38
  • 2
    I think the `BigDecimal` class may be useful. – MC Emperor May 27 '20 at 14:48
  • 2
    When you have expressions containing two different arithmetic types there is nothing as simple as you seem to be looking for. The easiest Java type that can function as a supertype for at least comparison operations is `BigDecimal`, as noted by @MCEmperor. The BigDecimal class can represent all the values of all the numeric types exactly, and can be easily constructed from them, and implements the `Comparable` interface. – President James K. Polk May 27 '20 at 15:07
  • By the way, Java does not have unsigned types (except for `char`). – President James K. Polk May 27 '20 at 16:23

2 Answers2

2

Is there a numerical type (primitive or class) in Java that contains every other numerical type?

No.

Starting with the primitive types:

  • a byte doesn't contain all short values
  • a short or char doesn't contain all int values.
  • an int doesn't contain all long values.
  • a long doesn't contain all float values.
  • a float doesn't contain all double values.

The above are evident from the ranges of the respective types.

  • a double doesn't contain all long values.

This is not quite so obvious. The range of double is larger than the range of long, but a double has only 53 bits of precision. If you do the math, this means that there are numbers (integer values) in the range of long that cannot have an exact representation as double values. Long.MAX_VALUE will be one example.

What about BigInteger and BigDecimal.

  • BigInteger does not include values in double that are not integers.

  • BigDecimal does not include values that correspond to the ±INF or ±NaN values in double


Can any number be converted into a Double in Java?

No, as explained above. Some long values do not have a corresponding double value. There there are BigInteger and BigDecimal values.

If not, is there a Number class that could parse any numerical type and that would provide basic mathematical operations?

If you are prepared to discount INF and NaN values, and use a large enough precision in the MathContext, then a BigDecimal should good enough ... within the limits of machine memory and of the implementation.

(The current BigInteger class has an architectural limit; see Is there an upper bound to BigInteger?)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

java.lang.Number is an abstract super class for many different number types. As such Java is a strongly typed language and expects proper data types at all time. It is true that sometimes it is necessary to convert it to a proper or different data types.

The devil is in the detail. Surely, java.lang.Number is a class that contains every other numerical type.

This seems to do the trick

Number number = NumberFormat.getInstance().parse(myString);

Courtesy Joakim https://stackoverflow.com/a/33018384/12011019

Ray Mo
  • 34
  • 3
  • Number doesn't implement Comparable, so I doubt it's what the OP is looking for. – President James K. Polk May 27 '20 at 15:09
  • Hi just quoting a previous thread here as to why Number is comparable. https://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html (the above code uses the Object and Text classes in a multi purpose NumberFormat class which can be used to identify locales. As an added bonus we can compare if the number is a decimal or not.:) ) – Ray Mo May 27 '20 at 16:21