java.lang.Object
io.github.tonywasher.joceanus.oceanus.decimal.OceanusNewDecimal

public class OceanusNewDecimal extends Object
Decimal class performing integer arithmetic on large decimals.
  • Field Details

    • MAX_DECIMALS

      public static final int MAX_DECIMALS
      The Maximum # of Decimals.
      See Also:
  • Constructor Details

    • OceanusNewDecimal

      public OceanusNewDecimal(int pScale)
      Constructor.
      Parameters:
      pScale - the number of decimal digits
    • OceanusNewDecimal

      public OceanusNewDecimal(BigDecimal pSource)
      Constructor.
      Parameters:
      pSource - the source BigDecimal
    • OceanusNewDecimal

      public OceanusNewDecimal(long pIntegral, int pFractional, int pSign, int pScale)
      Constructor.
      Parameters:
      pIntegral - the integral part of the decimal.
      pFractional - the fractional part of the decimal
      pSign - the sign of the decimal
      pScale - the number of decimal digits
  • Method Details

    • integralValue

      public long integralValue()
      Obtain the integral part of the decimal.
      Returns:
      the integral part of the decimal
    • signum

      public int signum()
      Obtain the sign of the decimal.
      Returns:
      -1, 0, or 1 as the value of this Decimal is negative, zero, or positive
    • fractionalValue

      public int fractionalValue()
      Obtain the fractional part of the decimal.
      Returns:
      the fractional part of the decimal
    • scale

      public int scale()
      Obtain the scale of the decimal.
      Returns:
      the scale of the decimal
    • add

      public void add(OceanusNewDecimal pDecimal)
      Add a decimal to value.
      Parameters:
      pDecimal - the decimal to add to this value
    • subtract

      public void subtract(OceanusNewDecimal pDecimal)
      Subtract a decimal from value.
      Parameters:
      pDecimal - the decimal to subtract from this value
    • multiply

      public void multiply(OceanusNewDecimal pMultiplicand)
      Multiply by another decimal

      This function splits the values into three separate integers and then performs long arithmetic to prevent loss of precision. The value is represented as (x,y,z,s) where the decimal may be written as x*232 + y + z*10-s and x,y,z,s are all integers.

      The product of (x1, y1, z1, s) by (x2, y2, z2, t) is therefore x1*x2*264 (discardable) + (x1*y2 + x2*y1)*232 + x2*y2 + x1*z2*232*10-t + x2*z1*232*10-s + y1*z2*10-t + y2*z1*10-s + z1*z2*10-s-t

      Parameters:
      pMultiplicand - the decimal to multiply by
    • divide

      public void divide(OceanusNewDecimal pDivisor)
      Divide by another decimal.

      This function uses BigDecimal to perform the calculation

      Parameters:
      pDivisor - the decimal to divide by
    • toBigDecimal

      public BigDecimal toBigDecimal()
      Convert to BigDecimal.
      Returns:
      the BigDecimal equivalent
    • toString

      public String toString()
      Overrides:
      toString in class Object