Class NormalisedDecimal

java.lang.Object
org.xlsx4j.org.apache.poi.ss.util.NormalisedDecimal

final class NormalisedDecimal extends Object
Represents a transformation of a 64 bit IEEE double quantity having a decimal exponent and a fixed point (15 decimal digit) significand. Some quirks of Excel's calculation behaviour are simpler to reproduce with numeric quantities in this format. This class is currently used to help:
  1. Comparison operations
  2. Conversions to text

This class does not handle negative numbers or zero.

The value of a NormalisedDecimal is given by
significand × 10decimalExponent
where:
significand = wholePart + fractionalPart / 224

  • Field Details

    • EXPONENT_OFFSET

      private static final int EXPONENT_OFFSET
      Number of powers of ten contained in the significand
      See Also:
    • BD_2_POW_24

      private static final BigDecimal BD_2_POW_24
    • LOG_BASE_10_OF_2_TIMES_2_POW_20

      private static final int LOG_BASE_10_OF_2_TIMES_2_POW_20
      log10(2)×220
      See Also:
    • C_2_POW_19

      private static final int C_2_POW_19
      219
      See Also:
    • FRAC_HALF

      private static final int FRAC_HALF
      the value of _fractionalPart that represents 0.5
      See Also:
    • MAX_REP_WHOLE_PART

      private static final long MAX_REP_WHOLE_PART
      1015
      See Also:
    • _relativeDecimalExponent

      private final int _relativeDecimalExponent
      The decimal exponent increased by one less than the digit count of _wholePart
    • _wholePart

      private final long _wholePart
      The whole part of the significand (typically 15 digits). 47-50 bits long (MSB may be anywhere from bit 46 to 49) LSB is units bit.
    • _fractionalPart

      private final int _fractionalPart
      The fractional part of the significand. 24 bits (only top 14-17 bits significant): a value between 0x000000 and 0xFFFF80
  • Constructor Details

    • NormalisedDecimal

      NormalisedDecimal(long wholePart, int fracPart, int decimalExponent)
  • Method Details

    • create

      public static NormalisedDecimal create(BigInteger frac, int binaryExponent)
    • roundUnits

      public NormalisedDecimal roundUnits()
      Rounds at the digit with value 10decimalExponent
    • normaliseBaseTwo

      public ExpandedDouble normaliseBaseTwo()
      Convert to an equivalent ExpandedDouble representation (binary frac and exponent). The resulting transformed object is easily converted to a 64 bit IEEE double:
      • bits 2-53 of the #getSignificand() become the 52 bit 'fraction'.
      • #getBinaryExponent() is biased by 1023 to give the 'exponent'.
      The sign bit must be obtained from somewhere else.
      Returns:
      a new NormalisedDecimal normalised to base 2 representation.
    • composeFrac

      BigInteger composeFrac()
      Returns:
      the significand as a fixed point number (with 24 fraction bits and 47-50 whole bits)
    • getSignificantDecimalDigits

      public String getSignificantDecimalDigits()
    • getSignificantDecimalDigitsLastDigitRounded

      public String getSignificantDecimalDigitsLastDigitRounded()
      Rounds the first whole digit position (considers only units digit, not frational part). Caller should check total digit count of result to see whether the rounding operation caused a carry out of the most significant digit
    • getDecimalExponent

      public int getDecimalExponent()
      Returns:
      the number of powers of 10 which have been extracted from the significand and binary exponent.
    • compareNormalised

      public int compareNormalised(NormalisedDecimal other)
      assumes both this and other are normalised
    • getFractionalPart

      public BigDecimal getFractionalPart()
    • getFractionalDigits

      private String getFractionalDigits()
    • toString

      public String toString()
      Overrides:
      toString in class Object