Class Decimal
The Decimal class is a helper class to perform decimal arithmetic in
scripts and to represent a decimal number with arbitrary length. The decimal
class avoids arithmetic errors, which are typical for calculating with
floating numbers, that are based on a binary mantissa.
The class is designed in a way that it can be used very similar to a
desktop calculator.
1 var d = new Decimal( 10.0 );
2 var result = d.add( 2.0 ).sub( 3.0 ).get();
The above code will return 9 as result.
Constructor Summary
Constructor Description Decimal ()Constructs a new Decimal with the value 0. Decimal (Number )Constructs a new decimal using the specified Number value. Decimal (BigInt )Constructs a new decimal using the specified BigInt value. Decimal (String )Constructs a new Decimal using the specified string representation of a number.
Method Summary
Method Description abs ()Returns a new Decimal with the absolute value of this Decimal. add (Number )Adds a Number value to this Decimal and returns the new Decimal. add (Decimal )Adds a Decimal value to this Decimal and returns the new Decimal. addPercent (Number )Adds a percentage value to the current value of the decimal. addPercent (Decimal )Adds a percentage value to the current value of the decimal. divide (Number )Divides the specified Number value with this decimal and returns the new decimal. divide (Decimal )Divides the specified Decimal value with this decimal and returns the new decimal. equals (Object )Compares two decimal values whether they are equivalent. get ()Returns the value of the Decimal as a Number. hashCode ()Calculates the hash code for this decimal; multiply (Number )Multiples the specified Number value with this Decimal and returns the new Decimal. multiply (Decimal )Multiples the specified Decimal value with this Decimal and returns the new Decimal. negate ()Returns a new Decimal with the negated value of this Decimal. round (Number )Rounds the current value of the decimal using the specified number of decimals. subtract (Number )Subtracts the specified Number value from this Decimal and returns the new Decimal. subtract (Decimal )Subtracts the specified Decimal value from this Decimal and returns the new Decimal. subtractPercent (Number )Subtracts a percentage value from the current value of the decimal. subtractPercent (Decimal )Subtracts a percentage value from the current value of the decimal. toString ()Returns a string representation of this object. valueOf ()The valueOf() method is called by the ECMAScript interpret to return the “natural” value of an object.
Methods inherited from class Object
assign , create , create , defineProperties , defineProperty , entries , freeze , fromEntries , getOwnPropertyDescriptor , getOwnPropertyNames , getOwnPropertySymbols , getPrototypeOf , hasOwnProperty , is , isExtensible , isFrozen , isPrototypeOf , isSealed , keys , preventExtensions , propertyIsEnumerable , seal , setPrototypeOf , toLocaleString , toString , valueOf , values
Constructor Details
Decimal()
Decimal()
Constructs a new Decimal with the value 0.
Decimal(Number)
Decimal(value: Number )
Constructs a new decimal using the specified Number value.
Parameters:
value - the value to use.
Decimal(BigInt)
Decimal(value: BigInt )
Constructs a new decimal using the specified BigInt value.
Parameters:
value - the value to use.
API Version:
Available from version 22.7.
Decimal(String)
Decimal(value: String )
Constructs a new Decimal using the specified string representation of
a number.
Parameters:
value - the value to use.
Method Details
abs()
abs(): Decimal
Returns a new Decimal with the absolute value of this Decimal.
Returns:
add(Number)
add(value: Number ): Decimal
Adds a Number value to this Decimal and returns the new Decimal.
Parameters:
value - the value to add to this decimal.
Returns:
the new decimal with the value added.
add(Decimal)
add(value: Decimal ): Decimal
Adds a Decimal value to this Decimal and returns the new Decimal.
Parameters:
value - the value to add to this decimal.
Returns:
the new decimal with the value added.
addPercent(Number)
addPercent(value: Number ): Decimal
Adds a percentage value to the current value of the
decimal. For example a value of 10 represent 10% or a value of
85 represents 85%.
Parameters:
value - the value to add.
Returns:
a new decimal with the added percentage value.
addPercent(Decimal)
addPercent(value: Decimal ): Decimal
Adds a percentage value to the current value of the
decimal. For example a value of 10 represent 10% or a value of
85 represents 85%.
Parameters:
value - the value to add.
Returns:
a new decimal with the added percentage value.
divide(Number)
divide(value: Number ): Decimal
Divides the specified Number value with this decimal and returns the new
decimal.
When performing the division, 34 digits precision and a rounding mode of
HALF_EVEN is used to prevent quotients with nonterminating decimal
expansions.
Parameters:
value - the value to use to divide this decimal.
Returns:
divide(Decimal)
divide(value: Decimal ): Decimal
Divides the specified Decimal value with this decimal and returns the new
decimal.
When performing the division, 34 digits precision and a rounding mode of
HALF_EVEN is used to prevent quotients with nonterminating decimal
expansions.
Parameters:
value - the value to use to divide this decimal.
Returns:
equals(Object)
equals(other: Object ): Boolean
Compares two decimal values whether they are equivalent.
Parameters:
other - the object to comapre against this decimal.
get()
get(): Number
Returns the value of the Decimal as a Number.
Returns:
the value of the Decimal.
hashCode()
hashCode(): Number
Calculates the hash code for this decimal;
multiply(Number)
multiply(value: Number ): Decimal
Multiples the specified Number value with this Decimal and returns the new Decimal.
Parameters:
value - the value to multiply with this decimal.
Returns:
multiply(Decimal)
multiply(value: Decimal ): Decimal
Multiples the specified Decimal value with this Decimal and returns the new Decimal.
Parameters:
value - the value to multiply with this decimal.
Returns:
negate()
negate(): Decimal
Returns a new Decimal with the negated value of this Decimal.
Returns:
round(Number)
round(decimals: Number ): Decimal
Rounds the current value of the decimal using the specified
number of decimals. The parameter
specifies the number of digest after the decimal point.
Parameters:
decimals - the number of decimals to use.
Returns:
the decimal that has been rounded.
subtract(Number)
subtract(value: Number ): Decimal
Subtracts the specified Number value from this Decimal and returns the new Decimal.
Parameters:
value - the value to add to this decimal.
Returns:
the new decimal with the value subtraced.
subtract(Decimal)
subtract(value: Decimal ): Decimal
Subtracts the specified Decimal value from this Decimal and returns the new Decimal.
Parameters:
value - the value to add to this decimal.
Returns:
the new decimal with the value subtraced.
subtractPercent(Number)
subtractPercent(value: Number ): Decimal
Subtracts a percentage value from the current value of the
decimal. For example a value of 10 represent 10% or a value of
85 represents 85%.
Parameters:
value - the value to subtract.
Returns:
a new decimal with the subtracted percentage value.
subtractPercent(Decimal)
subtractPercent(value: Decimal ): Decimal
Subtracts a percentage value from the current value of the
decimal. For example a value of 10 represent 10% or a value of
85 represents 85%.
Parameters:
value - the value to subtract.
Returns:
a new decimal with the subtracted percentage value.
toString()
toString(): String
Returns a string representation of this object.
Returns:
a string representation of this object.
valueOf()
valueOf(): Object
The valueOf() method is called by the ECMAScript interpret to return
the "natural" value of an object. The Decimal object returns its
current value as number. With this behavior script snippets can
be written like:
var d = new Decimal( 10.0 ); var x = 1.0 + d.add( 2.0 );
where x will be at the end 13.0.
Returns:
the value of this object.