Want more information ?. Try the Web Home Page for this document.

PSPDecAr Features Index and Examples

The links below cover the five major features of the PSPDec class which is embedded as the 'Num' object within each PSPDecAr object. Generally exactly the same issues apply to PSPDecAr and PSPDec.

PSPDecAr Introductory Code Example 1

PSPDecArExample1.java shows basic construction and conversion to/from other data types. Single parameter is numeric value e.g. 123.45

import uk.co.prosperosoftware.pspnum.*;
class PSPDecArExample1 {
    public static void main(String[] args) {
    if (args.length > 0) {
      // Construct PSPDecAr with 15 digits before and 2 digits after decimal place
      // Allow initial dimension of 3 elements
      PSPDecAr DecimalArrayOne = new PSPDecAr(15,2,3);
      // Change current element of array from first (default when PSPDecAr
      // constructed) to second.
      DecimalArrayOne.setElement(DecimalArrayOne.getBase() + 1);
      // Assign parameter string to current element of PSPDecAr and catch exception
      // if invalid parameter e.g. 123.A5. Note that this is assignment
      // without rounding - the alternative is the assignRound() method
      try {
         DecimalArrayOne.Num.assign(args[0]);
      }
      catch (PSPException e) {
        int ErrNum = e.getErrNum();
        if (ErrNum == PSPException.BadStringNumber) {
          System.out.println("Bad number in parameter string " + args[0]);
        }
        else {
          System.out.println("Other error number " + ErrNum + " detected");
        }
      }
      // Advise value of PSPDecAr (current element and all elements).
      System.out.println("Value of PSPDecAr current element is " + DecimalArrayOne.Num.toString());
      System.out.println("Value of PSPDecAr all " + DecimalArrayOne.getDimension() + " elements is "
                          + DecimalArrayOne.toString());
    }
  }
}

Compile the above code and then try the following at a command line:

C:\>java PSPDecArExample1 1234567890.123
Value of PSPDecAr current element is 1234567890.12
Value of PSPDecAr all 3 elements is 0.00;1234567890.12;0.00

C:\>java PSPDecArExample1 1234567890123E-3
Value of PSPDecAr current element is 1234567890.12
Value of PSPDecAr all 3 elements is 0.00;1234567890.12;0.00

C:\>java PSPDecArExample1 123456789K.123
Bad number in parameter string 123456789K.123
Value of PSPDecAr current element is 0.00
Value of PSPDecAr all 3 elements is 0.00;0.00;0.00

PSPDecAr Introductory Code Example 2

PSPDecArExample2.java shows arithmetic and logical operations for PSPDecAr. Two parameters expected are both numeric values e.g. 123.45

import uk.co.prosperosoftware.pspnum.*;
class PSPDecArExample2 {
    public static void main(String[] args) {
    if (args.length > 1) {
      // Construct a PSPDecAr with 15 digits before and 2 digits after decimal place
      // and dimension 3
      PSPDecAr DecimalArray = new PSPDecAr(15,2,3);
      // Assign first parameter value with rounding to every element of array
      DecimalArray.assignRound(args[0]);
      System.out.println("DecimalArray has size (" + DecimalArray.getDigitsBeforeDP()
      + "," + DecimalArray.getDigitsAfterDP() + ") dimension " + DecimalArray.getDimension()
      + " value " + DecimalArray.toString());
      // Construct a PSPDec with size to fit second parameter
      PSPDec Decimal = new PSPDec(args[1]);
      System.out.println("Decimal has size (" + Decimal.getDigitsBeforeDP()
      + "," + Decimal.getDigitsAfterDP() + ") value " + Decimal.toString());
      // Basic arithmetic
      DecimalArray.divideRound("10");
      System.out.println("DecimalArray all elements divided by 10 : " + DecimalArray.toString());
      DecimalArray.Num.multiply(Decimal);
      System.out.println("DecimalArray current element then multiplied by Decimal : " +
                          DecimalArray.toString());
      // Compare numeric value
      boolean compare = DecimalArray.isGT(Decimal);
      System.out.println("All elements of DecimalArray greater than " + Decimal.toString() +
                         " : " + compare);
      compare = DecimalArray.Num.isPositive();
      System.out.println("DecimalArray current element is positive : " + compare);
    }
  }
}

Compile the above code and then try the following at a command line:

C:\>java PSPDecArExample2 123.45 789.54-
DecimalArray has size (15,2) dimension 3 value 123.45;123.45;123.45
Decimal has size (3,2) value -789.54
DecimalArray all elements divided by 10 : 12.35;12.35;12.35
DecimalArray current element then multiplied by Decimal : -9750.81;12.35;12.35
All elements of DecimalArray greater than -789.54 : false
DecimalArray current element is positive : false