Decimal
The Decimal
class represents arbitrary-precision floating-point numbers. With the standard
preset, it inherits from big.js
, which may result in inconsistencies in the output of methods like .toString()
across different presets.
Import
import { Decimal } from "@tai-kun/surrealdb/decodeonly-datatypes";import { Decimal } from "@tai-kun/surrealdb/encodable-datatypes";import { Decimal } from "@tai-kun/surrealdb/standard-datatypes";
.constructor()
Creates a new Decimal
object.
new Decimal(source: string); // decode-only, encodablenew Decimal(source: string | number | Decimal); // standard
Arguments
source
A string representation of a floating-point number. Numeric values are also accepted with the standard
preset.
Return Value
When called with new
, Decimal
returns a new instance of itself.
.toString()
instance method >=decode-only
Gets the string representation of the decimal number.
Arguments
None.
Return Value
Returns a string representation of the decimal number.
.toSurql()
instance method >=encodable
Converts the Decimal
object into a string suitable for embedding in SurrealQL queries. Similar to .toString()
, but adds a dec
suffix to inform the query parser that the string represents a decimal
type.
.toSurql(): string;
Arguments
None.
Return Value
Returns the decimal number with a dec
suffix.
Example
import { Decimal } from "@tai-kun/surrealdb/encodable-datatypes";
const decimal = new Decimal("3.14");console.log(decimal.toSurql());//-> 3.14dec
.clone()
instance method =standard
Creates a copy of the Decimal
object.
.clone(): this;
Arguments
None.
Return Value
Returns a new Decimal
object. If the Decimal
class is inherited, an instance of that inheriting class is returned.
Other Methods
When using the standard
preset, the Decimal
class inherits from the big.js
Big
class. Refer to the big.js
documentation for its instance methods and properties:
Advanced Usage
Generic Decimal
Object Type Checking
To reliably check if a value is a Decimal
object regardless of the preset, it’s recommended to use the isDecimal
function. This function avoids using instanceof
, making it suitable for situations where the preset of the value being checked is unknown.
import { isDecimal } from "@tai-kun/surrealdb";