4 Replies Latest reply: Aug 19, 2008 2:07 PM by 795426 RSS

    failure to convert infinite double to BigDecimal

    3004
      I need to convert double to BigDecimal. But one of the double values is infinite and is not converted into the BigDecimal.

      I am getting this error.

      java.lang.NumberFormatException: Infinite or NaN.

      This is how I am converting it.

      private BigDecimal convert(Double object) {
      return new BigDecimal(object);
      }

      Does anybody know how to do this?

      Thanks.
        • 1. Re: failure to convert infinite double to BigDecimal
          JosAH
          Where did that +inf or -inf come from? It most likely is an error somwhere else and you don't want to convert it to a BigDecimal at all.

          kind regards,

          Jos
          • 2. Re: failure to convert infinite double to BigDecimal
            807589
            Try this
            String str = object.toString();
            return new BigDecimal(str);
            • 3. Re: failure to convert infinite double to BigDecimal
              3004
              Unfortunately. it's not working.

              I am getting this.

              Caused by: java.lang.NumberFormatException
              at java.math.BigDecimal.<init>(BigDecimal.java:368)
              at java.math.BigDecimal.<init>(BigDecimal.java:647)


              What I want to do is to retrieve data from the db table and insert it into the target table. The issue is the data type conversion.

              It seems to me that BigDecimal can't handle infinite number.
              • 4. Re: failure to convert infinite double to BigDecimal
                795426
                caesarkim1 wrote:
                It seems to me that BigDecimal can't handle infinite number.
                Correct. As per the documentation in BigDecimal(double) and BigDecimal(String)
                BigDecimal(double)
                Throws: NumberFormatException - if val is infinite or NaN.
                BigDecimal(String)Translates the string representation of a BigDecimal into a BigDecimal. The string representation consists of an optional sign, '+' ('\u002B') or '-' ('\u002D'), followed by a sequence of zero or more decimal digits ("the integer"), optionally followed by a fraction, optionally followed by an exponent

                Throws: NumberFormatException - if val is not a valid representation of a BigDecimal