The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison and format conversion. The #toString method provides a canonical representation of a BigDecimal.

Constructor

new

BigDecimal

(Integer or Number unscaledValue, Number scale)

Constructs an immutable arbitrary-precision signed decimal number. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. The value of the number represented by the BigDecimal is therefore (unscaledValue × 10-scale).

Parameters:
Name Type Description
unscaledValue Integer or Number

The integer part of the decimal.

scale Number

The scale of the decimal.

Methods

add

(BigDecimal other)

Returns the sum of this and the given BigDecimal.

Parameters:
Name Type Description
other BigDecimal

The BigDecimal to sum to this.

Returns:
Type Description
BigDecimal

The BigDecimal result.

compare

(BigDecimal other)

Compares this BigDecimal with the given one.

Parameters:
Name Type Description
other BigDecimal

Integer to compare against.

Returns:
Type Description
number

0 if they are the same, 1 if the this is greater, and -1 if the given one is greater.

equals

(BigDecimal other)

Returns true if the value of the BigDecimal instance and other are the same

Parameters:
Name Type Description
other BigDecimal
Returns:
Type Description
Boolean

BigDecimal.fromBuffer

(Buffer buf)

Returns the BigDecimal representation of a buffer composed of the scale (int32BE) and the unsigned value (varint BE)

Static
This function is static
Parameters:
Name Type Description
buf Buffer
Returns:
Type Description
BigDecimal

BigDecimal.fromNumber

(Number value)

Returns a BigDecimal representation of the Number

Static
This function is static
Parameters:
Name Type Description
value Number
Returns:
Type Description
BigDecimal

BigDecimal.fromString

(String value)

Returns a BigDecimal representation of the string

Static
This function is static
Parameters:
Name Type Description
value String
Returns:
Type Description
BigDecimal

greaterThan

(BigDecimal other)

Returns true if the current instance is greater than the other

Parameters:
Name Type Description
other BigDecimal
Returns:
Type Description
boolean

isNegative

()
Returns:
Type Description
boolean

Whether this value is negative.

isZero

()
Returns:
Type Description
boolean

Whether this value is zero.

notEquals

(BigDecimal other)
Parameters:
Name Type Description
other BigDecimal
Returns:
Type Description
boolean

subtract

(BigDecimal other)

Returns the difference of this and the given BigDecimal.

Parameters:
Name Type Description
other BigDecimal

The BigDecimal to subtract from this.

Returns:
Type Description
BigDecimal

The BigDecimal result.

BigDecimal.toBuffer

(BigDecimal value)

Returns a buffer representation composed of the scale as a BE int 32 and the unsigned value as a BE varint

Static
This function is static
Parameters:
Name Type Description
value BigDecimal
Returns:
Type Description
Buffer

toJSON

()

Returns the string representation. Method used by the native JSON.stringify() to serialize this instance.

toNumber

()

Returns a Number representation of this BigDecimal.

Returns:
Type Description
Number

toString

()

Returns the string representation of this BigDecimal

Returns:
Type Description
string