public static void serializeHiveDecimal(
      ByteStream.Output buffer, HiveDecimalWritable decWritable, boolean invert,
      byte[] scratchBuffer) {

      // Get the sign of the decimal.
      int signum = decWritable.signum();

      // Get the 10^N power to turn digits into the desired decimal with a possible
      // fractional part.
      // To be compatible with the OldHiveDecimal version, zero has factor 1.
      int factor;
      if (signum == 0) {
        factor = 1;
      } else {
        factor = decWritable.rawPrecision() - decWritable.scale();
      }

      // To make comparisons work properly, the "factor" gets the decimal's sign, too.
      factor = signum == 1 ? factor : -factor;

      // Convert just the decimal digits (no dot, sign, etc) into bytes.
      //
      // This is much faster than converting the BigInteger value from unscaledValue() which is no
      // longer part of the HiveDecimal representation anymore to string, then bytes.
      int index = decWritable.toDigitsOnlyBytes(scratchBuffer);

      /*
       * Finally write out the pieces (sign, power, digits)
       */
      writeByte(buffer, (byte) ( signum + 1), invert);
      writeByte(buffer, (byte) ((factor >> 24) ^ 0x80), invert);
      writeByte(buffer, (byte) ( factor >> 16), invert);
      writeByte(buffer, (byte) ( factor >> 8), invert);
      writeByte(buffer, (byte)   factor, invert);

      // The toDigitsOnlyBytes stores digits at the end of the scratch buffer.
      serializeBytes(
          buffer,
          scratchBuffer, index, scratchBuffer.length - index,
          signum == -1 ? !invert : invert);
  }
Decimals are encoded in three pieces:Decimals are encoded in three pieces:

Sign:   1, 2 or 3 for smaller, equal or larger than 0 respectively
Factor: Number that indicates the amount of digits you have to move
        the decimal point left or right until the resulting number is smaller
        than zero but has something other than 0 as the first digit.
Digits: which is a string of all the digits in the decimal. If the number
        is negative the binary string will be inverted to get the correct ordering.

UNDONE: Is this example correct?
  Example: 0.00123
  Sign is 3 (bigger than 0)
  Factor is -2 (move decimal point 2 positions right)
  Digits are: 123

@param buffer
@param dec
@param invert
@param scratchBuffer
serde/src/java/org/apache/hadoop/hive/serde2/binarysortable/BinarySortableSerDe.java