public void writeDecimal(HiveDecimalWritable writable, int byteNum, int scale) throws IOException {
    if (writable == null) {
      byte[] pad = new byte[byteNum];
      write(pad);
      return;
    }
    // since the HiveDecimal will auto adjust the scale to save resource
    // we need to adjust it back otherwise the output bytes will be wrong
    int hiveScale = writable.getHiveDecimal().scale();
    BigInteger bigInteger = writable.getHiveDecimal().unscaledValue();
    if (hiveScale < scale) {
      BigInteger multiplicand = new BigInteger("1" + join("", Collections.nCopies(scale - hiveScale, "0")));
      bigInteger = bigInteger.multiply(multiplicand);
    }
    byte[] content = bigInteger.toByteArray();
    int signBit = content[0] >> 7 & 1;
    ArrayUtils.reverse(content);
    write(content);
    if (byteNum > content.length) {
      byte[] pad;
      if (signBit == 0) {
        pad = new byte[byteNum - content.length];
      } else {
        pad = new byte[byteNum - content.length];
        Arrays.fill(pad, (byte) 255);
      }
      write(pad);
    }
  }
Write DECIMAL(P, S).
The representation of decimal in Teradata binary format is:
the byte number to read is decided solely by the precision(P),
HiveDecimal is constructed through the byte array and scale.
the rest of byte will use 0x00 to pad (positive) and use 0xFF to pad (negative).
the null DECIMAL will use 0x00 to pad.

@param writable the writable
@param byteNum the byte num
@throws IOException the io exception
serde/src/java/org/apache/hadoop/hive/serde2/teradata/TeradataBinaryDataOutputStream.java