Returns -1
, 0
, and 1
if this
is less than, equal to, and greater
than other
, respectively.
Returns true if this
is an equivalent fraction to other
.
Returns true if this
is greater than or equal to other
.
Returns true if this
is greater than other
.
Returns true if this
is less than or equal to other
.
Returns true if this
is less than other
.
Returns true if this
is not an equivalent fraction to other
.
Returns a fractional approximate of this
that has the specified
denominator. This method rounds the numerator using the specified rounding
mode if it is not divisible by the new denominator.
See RoundingMode for rounding mode options.
let f = BigAmount.create("1/3"); // 1/3 = 0.333333...
f.quantize(100n); // 33/100 = 0.33
Note that the RoundingMode applies to the resulting numerator; the outcome of "toward positive / negative" is determined by the sign of numerator, which could be counterintuitive when the new denominator is negative.
Returns a fractional approximate of this
that is rounded to the multiple
of 1 / (10 ** ndigits)
, just like Python's built-in round()
. This
method rounds ties to even by default.
Number of digits after the decimal separator.
See RoundingMode for rounding mode options.
Returns an integral approximate of this
, rounding ties to even by
default.
See RoundingMode for rounding mode options.
Formats a BigAmount using decimal exponential notation just like
Number#toExponential
. Unlike Number#toExponential
, this method always
requires the first argument.
Number of digits to appear after the decimal separator.
Formats a BigAmount using decimal fixed-point notation just like
Number#toFixed
. In addition, this method takes format options to
customize the output. See FormatOptions for options and examples.
Number of digits to appear after the decimal separator.
Optional
formatOptions: FormatOptionsSame as BigAmount.quantize but returns undefined
if the numerator
needs to be rounded.
Static
createCreates a BigAmount from various arguments. For convenience, this
method is also exported as q and is callable as q(x)
and
q(x, y)
.
bigint
| number
| string
| { num: bigint | number | string; den: bigint | number | string }
Optional
y: BigAmountLikebigint
| number
| string
| { num: bigint | number | string; den: bigint | number | string }
q(123n); // 123/1
q(123); // 123/1
q("123"); // 123/1
q("123.45"); // 12345/100
q("123.45e-6"); // 12345/100000000
q("123/45"); // 123/45
q("12.3/-4.5"); // 1230/-450
Note that non-integral number
values have to be passed as string
.
q(123.45); // ERROR!
q(123 / 45); // ERROR!
q(123n, 45n); // 123/45
q(123, 45); // 123/45
q(123, 45n); // 123/45
q(123n, "4.5"); // 1230/45
q("1/2", "3/4"); // 4/6
q("1/2", "3.4"); // 10/68
This method accepts the following BigAmount-like arguments:
bigint
- Any bigint
value.number
- Integer only. This is because it is often imprecise and
expensive to find a rational approximate of a non-integral number. Pass
the number as a string (e.g. "1/3"
, "1.23"
) to create an exact value
or use BigAmount.fromNumber to find an approximate.string
- Fraction ("1/23"
), integer ("123"
, "0xFF"
), decimal
("-1.23"
, ".123"
), or scientific ("1.23e-4"
, "-12e+3"
). The
fractional notation q("num/den")
is equivalent to q("num", "den")
.object
- Any object (including any BigAmount value) that has
two BigAmount-like scalar fields named num
and den
. q({ num: x, den: y })
is equivalent to q(x, y)
, except that the fields do not accept
an object.Static
fromCreates a BigAmount from number
. Unlike BigAmount.create,
this method finds a rational approximate of a non-integral number.
Static
sumCreates a BigAmount instance of the sum of list items.
Array of values that BigAmount.create accepts.
BigAmount.sum(["123/100", "-456/100", "789/100"]); // 456/100
Adds other
to this
, keeping the denominator unchanged. This method is
equivalent to f.add(other).quantize(f.den, roundingMode)
.
Divides this
by other
, keeping the denominator unchanged. This method
is equivalent to f.div(other).quantize(f.den, roundingMode)
.
Multiplies this
by other
, keeping the denominator unchanged. This
method is equivalent to f.mul(other).quantize(f.den, roundingMode)
.
Subtracts other
from this
, keeping the denominator unchanged. This
method is equivalent to f.sub(other).quantize(f.den, roundingMode)
.
Divides this
by other
, resetting the denominator to newDen
. This
method is equivalent to f.div(other).quantize(newDen, roundingMode)
and
is typically useful to divide a dollar amount by quantity to calculate the
unit price at a specific precision.
Multiplies this
by other
, resetting the denominator to newDen
. This
method is equivalent to f.mul(other).quantize(newDen, roundingMode)
and
is typically useful to multiply a quantity by unit price to calculate the
dollar amount at a specific precision.
Readonly
denRaw denominator.
Readonly
numRaw numerator.
Example