Skip to content

Add ceil, floor, round and trunc

Michael M edited this page Jan 9, 2019 · 6 revisions

my-bignumber.mjs

import BigNumber from "./bignumber.mjs"

BigNumber.set({
    DECIMAL_PLACES: 40,
    ROUNDING_MODE: BigNumber.ROUND_HALF_UP,
    EXPONENTIAL_AT: [-20, 30],
    RANGE: 1E9,
    CRYPTO: true,
    MODULO_MODE: BigNumber.ROUND_FLOOR,
    POW_PRECISION: 80,
    FORMAT: {
      decimalSeparator: '.',
      groupSeparator: ',',
      groupSize: 3,
      secondaryGroupSize: 0,
      fractionGroupSeparator: '',
      fractionGroupSize: 0
    },
    ALPHABET: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_'
  });
  
BigNumber.prototype.ceil = function () {
  return this.integerValue(BigNumber.ROUND_CEIL);
};

BigNumber.prototype.floor = function () {
  return this.integerValue(BigNumber.ROUND_FLOOR);
};

BigNumber.prototype.round = function () {
  return this.integerValue(BigNumber.ROUND_HALF_CEIL);
};

BigNumber.prototype.trunc = function () {
  return this.integerValue(BigNumber.ROUND_DOWN);
};

export default BigNumber;

main.mjs

import BigNumber from "./my-bignumber.mjs"

let x = new BigNumber('43253.589807643345');
x.ceil();            // '43254'
Clone this wiki locally