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

PSPDec Features Index and Examples

The six major features of PSPDec which you need to understand are:

PSPDec Introductory Code Example 1

PSPDecExample1.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 PSPDecExample1 {
    public static void main(String[] args) {
    if (args.length > 0) {
      // Construct PSPDec with 15 digits before and 2 digits after decimal place
      PSPDec DecimalOne = new PSPDec(15,2);
      // Assign parameter string to this PSPDec and catch exception
      // if invalid parameter e.g. 123.A5. Note that this is assignment
      // without rounding - the alternative is the assignRound() method
      try {
         DecimalOne.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 current value of PSPDec as various data types.
      System.out.println("Current value of PSPDec as String is " + DecimalOne.toString());
      System.out.println("Current value of PSPDec as long   is " + DecimalOne.longValue());
      System.out.println("Current value of PSPDec as double is " + DecimalOne.doubleValue());
    }
  }
}

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

C:\>java PSPDecExample1 1234567890.123
Current value of PSPDec as String is 1234567890.12
Current value of PSPDec as long   is 1234567890
Current value of PSPDec as double is 1.23456789012E9

C:\>java PSPDecExample1 1234567890123E-3
Current value of PSPDec as String is 1234567890.12
Current value of PSPDec as long   is 1234567890
Current value of PSPDec as double is 1.23456789012E9

C:\>java PSPDecExample1 123456789K.123
Bad number in parameter string 123456789K.123
Current value of PSPDec as String is 0.00
Current value of PSPDec as long   is 0
Current value of PSPDec as double is 0.0

PSPDec Introductory Code Example 2

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

import uk.co.prosperosoftware.pspnum.*;
class PSPDecExample2 {
    public static void main(String[] args) {
    if (args.length > 1) {
      // Construct first PSPDec with 15 digits before and 2 digits after decimal place
      // assign first parameter value with rounding
      PSPDec DecimalOne = new PSPDec(15,2);
      DecimalOne.assignRound(args[0]);
      System.out.println("DecimalOne has size (" + DecimalOne.getDigitsBeforeDP()
      + "," + DecimalOne.getDigitsAfterDP() + ") value " + DecimalOne.toString());
      // Construct second PSPDec with size to fit second parameter
      PSPDec DecimalTwo = new PSPDec(args[1]);
      System.out.println("DecimalTwo has size (" + DecimalTwo.getDigitsBeforeDP()
      + "," + DecimalTwo.getDigitsAfterDP() + ") value " + DecimalTwo.toString());
      // Compare numeric value
      boolean compare = DecimalOne.isGT(DecimalTwo);
      System.out.println("DecimalOne is greater than DecimalTwo : " + compare);
      compare = DecimalOne.isPositive();
      System.out.println("DecimalOne is positive : " + compare);
      // Basic arithmetic
      DecimalOne.divideRound("10");
      System.out.println("DecimalOne divided by 10 : " + DecimalOne.toString());
      DecimalOne.multiply(DecimalTwo);
      System.out.println("DecimalOne then multiplied by DecimalTwo : " + DecimalOne.toString());
    }
  }
}

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

C:\>java PSPDecExample2 123.45 789.54-
DecimalOne has size (15,2) value 123.45
DecimalTwo has size (3,2) value -789.54
DecimalOne is greater than DecimalTwo : true
DecimalOne is positive : true
DecimalOne divided by 10 : 12.35
DecimalOne then multiplied by DecimalTwo : -9750.81