compressAmount static method

HermezCompressedAmount compressAmount(
  1. double f
)

Convert a fix to a float @param {String} _f - Scalar encoded in fix @returns {HermezCompressedAmount} HermezCompressedAmount representation of the amount

Implementation

static HermezCompressedAmount compressAmount(double f) {
  if (f.sign == 0) {
    return new HermezCompressedAmount(0);
  }

  var m = f;
  var e = 0;

  while ((m % 10).floor().sign == 0 && (m / 0x800000000).floor().sign != 0) {
    m = m / 10;
    e++;
  }

  if (e > 31) {
    throw new ArgumentError("number too big");
  }

  if ((m / 0x800000000).floor().sign != 0) {
    throw new ArgumentError("not enough precision");
  }

  final res = m + (e * 0x800000000);

  return new HermezCompressedAmount(res);
}