Decimal arithmetic
Copyright (c) IBM Corporation, 2000. All rights reserved. ©
11 Jan 2000
[previous | contents | next]

Constructors

These constructors create an object of type BigDecimal from some other object or primitive value. In all cases except construction from a double value (for which exact conversion is often not possible) sufficient digits are used to represent the original value exactly.

BigDecimal(char[])

Constructs a BigDecimal as though the character array had been copied to a String and the BigDecimal(String) constructor had then been used. The parameter must not be null.

BigDecimal(char[], int, int)

Constructs a BigDecimal from a subarray of characters. The first parameter is the array in which the subarray is to be found, and the other parameters specify its offset and length respectively.

The BigDecimal is constructed as though the subarray had been copied to a String and the BigDecimal(String) constructor had then been used. The first parameter must not be null, and the subarray must be wholly contained within the array.

BigDecimal(double)

Constructs a BigDecimal which is a decimal representation of the 64-bit signed binary floating point parameter. If the parameter is infinite, or is not a number (NaN), a NumberFormatException is thrown.

Note: this constructor is deprecated as it does not give the same result as converting the double to a String using the Double.toString() method and then using the BigDecimal(String) constructor. Instead, use the static valueOf(double) method to construct a BigDecimal from a double (or from a float).

BigDecimal(int)

Constructs a BigDecimal which is the exact decimal representation of the 32-bit signed binary integer parameter. The BigDecimal will contain only decimal digits, prefixed with a leading minus sign (hyphen) if the parameter is negative. A leading zero will be present only if the parameter is zero.

BigDecimal(java.math.BigDecimal)

Constructs a BigDecimal as though the parameter had been represented as a String (using its toString method) and the BigDecimal(java.lang.String) constructor (see below) had then been used. The parameter must not be null.

(Note: this constructor is provided only in the com.ibm.math version of the BigDecimal class. It would not be present in a java.math version.)

BigDecimal(java.math.BigInteger)

Constructs a BigDecimal which is the exact decimal representation of the BigInteger parameter. The parameter must not be null.

The BigDecimal will contain only decimal digits, prefixed with a leading minus sign (hyphen) if the BigInteger is negative. A leading zero will be present only if the BigInteger is zero.

BigDecimal(java.math.BigInteger, int)

Constructs a BigDecimal which is the exact decimal representation of the BigInteger, scaled by the second parameter (the scale). The value of the BigDecimal is the BigInteger divided by ten to the power of the scale. The BigInteger parameter must not be null.

The BigDecimal will contain only decimal digits (with an embedded decimal point followed by scale decimal digits if the scale is positive), prefixed with a leading minus sign (hyphen) if the BigInteger is negative. A leading zero will be present only if the BigInteger is zero.

A NumberFormatException is thrown if the scale is negative.

BigDecimal(java.lang.String)

Constructs a BigDecimal from the parameter, which must represent a valid number. The parameter must not be null.

In summary, numbers in String form must have at least one digit, may have a leading sign, may have a decimal point, and exponential notation may be used. They follow conventional syntax, and may not contain blanks.

Some valid Strings from which a BigDecimal might be constructed are:

       "0"         /* Zero                         */
      "12"         /* A whole number               */
     "-76"         /* A signed whole number        */
      "12.70"      /* Some decimal places          */
      "+0.003"     /* A plus sign is allowed, too. */
      "17."        /* The same as 17               */
        ".5"       /* The same as 0.5              */
      "4E+9"       /* Exponential notation         */
       "0.73e-7"   /* Exponential notation         */
(Exponential notation means that the number includes an optional sign and a power of ten following an 'E' that indicates how the decimal point will be shifted. Thus the "4E+9" above is just a short way of writing 4000000000, and the "0.73e-7" is short for 0.000000073.)

The BigDecimal constructed from the String is in a standard form, as though the add method had been used to add zero to the number with unlimited precision.[1] 

If the string uses exponential notation (that is, includes an e or an E), then the BigDecimal number will be expressed in scientific notation (where the power of ten is adjusted so there is a single non-zero digit to the left of the decimal point). In this case if the number is zero then it will be expressed as the single digit 0, and if non-zero it will have an exponent unless that exponent would be 0. The exponent must fit in nine digits both before and after it is expressed in scientific notation.

Any digits in the parameter must be decimal; that is:

  java.lang.Character.digit(c, 10)
(where c is the character in question) would not return -1.

A NumberFormatException is thrown if the parameter is not a valid number or the exponent will not fit in nine digits.

BigDecimal(long)

Constructs a BigDecimal which is the exact decimal representation of the 64-bit signed binary integer parameter. The BigDecimal will contain only decimal digits, prefixed with a leading minus sign (hyphen) if the parameter is negative. A leading zero will be present only if the parameter is zero.


Footnotes:
[1] That is, with a digits setting of 0.

[previous | contents | next]