Skip to main content

BigDecimal

A collection of functions to manipulate BigDecimal

Index

Codec

__decode__

  • __decode__(this, input, context): Result<BigDecimal, CodecError>
  • Returns the decoded input, Result.Ok or Result.Error()

    interface SomeObject {
    foo: string
    }
    const someCodec: Codec<SomeObject> = ...;
    const input: unknown = ...;
    const decoded = Codec.decode(someCodec, input);

__encode__

  • __encode__(this, input): unknown
  • Returns the encoded input

    interface SomeObject {
    foo: string
    }
    const someCodec: Codec<SomeObject> = ...;
    const someObject: SomeObject = { foo: "bar" }
    const encoded = Codec.decode(someCodec, someObject);

__schema__

  • __schema__(this): JSONValue
  • Returns the JSONSchema corresponding to the decoded type

    const someCodec: Codec<unknown> = ...;
    const jsonSchema = Codec.schema(someCodec);

Comparator

!=

  • !=(this, left, right): boolean
  • "Not equal to" operator

    const TEqual: Equal<T>;
    TEqual['!='](value, otherValue); // true
    TEqual['!='](value, value); // false

  • (this, left, right): boolean
  • "Less than" operator

    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['<'](smallerT, smallerT); // false
    TCompare['<'](smallerT, greaterT); // true
    TCompare['<'](greaterT, smallerT); // false

<=

  • <=(this, left, right): boolean
  • "Less than or equal to" operator

    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['<='](smallerT, smallerT); // true
    TCompare['<='](smallerT, greaterT); // true
    TCompare['<='](greaterT, smallerT); // false

==

  • ==(this, left, right): boolean
  • "Equal to" operator

    type T = // ...
    const TEqual: Equal<T>;
    TEqual['=='](value, value); // true
    TEqual['=='](value, otherValue); // false

  • (this, left, right): boolean
  • "Greater than" operator

    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['>'](smallerT, smallerT); // false
    TCompare['>'](smallerT, greaterT); // false
    TCompare['>'](greaterT, smallerT); // true

>=

  • >=(this, left, right): boolean
  • "Greater than or equal to" operator

    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['>='](smallerT, smallerT); // true
    TCompare['>='](smallerT, greaterT); // false
    TCompare['>='](greaterT, smallerT); // true

clamp

  • clamp(this, value, minValue, maxValue): BigDecimal
  • Clamp value between minValue and maxValue

    type T;
    const TCompare: Comparable<T>;
    TCompare.clamp(value, min, max); // min if value < min, max if value > max, otherwise value itself

compare

  • compare(this, left, right): Ordering
  • Return an Ordering that represents comparison result

    type T;
    const TCompare: Comparable<T>;
    const sorted = [3, 1, 1].sort(TCompare.compare);

    See more at Order

equals

  • equals(this, left, right): boolean
  • Alias to '=='

    type T = // ...
    const TEqual: Equal<T>;
    TEqual.equals(value, value); // true
    TEqual.equals(value, otherValue); // false

max

  • max(this, left, right): BigDecimal
  • "maximum" operator

    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare.max(smallerT, greaterT); // greaterT

min

  • min(this, left, right): BigDecimal
  • "minimum" operator

    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare.min(smallerT, greaterT); // smallerT

Constructor

create

  • create(this, properties): BigDecimal
  • Construct a new model

fromBigInt

  • fromBigInt(bigIntValue): BigDecimal
  • Converts a bigint to a big decimal.

    BigDecimal.fromBigInt(1n); // BigDecimal('1')
    BigDecimal.fromBigInt(-1n); // BigDecimal('-1')

fromInt

  • fromInt(intValue): BigDecimal
  • Converts an integer to a big decimal.

    BigDecimal.fromInt(Int(1)); // BigDecimal('1')
    BigDecimal.fromInt(Int(-1)); // BigDecimal('-1')

of

  • of(value, scale): BigDecimal
  • Returns a new BigDecimal from value and scale

    BigDecimal.of(1n, 1); // BigDecimal('0.1')
    BigDecimal.of(-234n, 2); // BigDecimal('2.34')

Formatting

asString

  • asString(this, self): string
  • Converts the given value to a String.

    Number.asString(123); // '123'

Numeric

-

  • -(left, right): BigDecimal
  • Subtraction operator

    type T = ...;
    const TNumeric: Numeric.Subtract<T> = ...;
    const result = Numeric['-'](left, right);// represents (left - right)

*

  • *(left, right): BigDecimal
  • Multiplication operator

    type T = ...;
    const TNumeric: Numeric.Multiply<T> = ...;
    const result = Numeric['*'](left, right);// represents (left * right)

%?

  • %?(left, right): Option<BigDecimal>
  • Remainder operator

    type T = ...;
    const TNumeric: Numeric.CheckedRemainder<T> = ...;
    const result = Numeric['%?'](left, right);// represents (left % right)

+

  • +(left, right): BigDecimal
  • Addition operator

    type T = ...;
    const TNumeric: Numeric.Add<T> = ...;
    const result = TNumeric['+'](left, right);// represents (left + right)

abs

  • abs(this, value): BigDecimal
  • Absolute value. It should satisfy Numeric['*'](Numeric.abs(x), Numeric.sign(x)) == x

    type T = ...;
    const TSigned: Numeric.Signed<T> = ...;
    const result = TSigned.abs(value);// absolute value of (value)

isNegative

  • isNegative(this, self): boolean
  • Returns true if the number is negative and false if the number is zero or positive.

    Number.isPositive(-1);// true
    Number.isPositive(0);// false
    Number.isPositive(1);// false

isPositive

  • isPositive(this, self): boolean
  • Returns true if the number is positive and false if the number is zero or negative.

    Number.isPositive(-1);// false
    Number.isPositive(0);// false
    Number.isPositive(1);// true

isZero

  • isZero(this, self): boolean
  • Returns true if self is equal to the additive identity.

    Number.isZero(0); // true
    Number.isZero(1); // false

negate

  • negate(this, self): BigDecimal
  • Negates the given value.

    Number.negate(5); // -5
    Number.negate(Number.negate(5)); // 5

sign

  • sign(this, value): BigDecimal
  • Sign of a number. It should satisfy TSigned['*'](TSigned.abs(x), TSigned.sign(x)) == x

    Number.sign(-2.5);// -1
    Number.sign(0);// 0
    Number.sign(2.5);// 1

truncate

  • truncate(self, scale): BigDecimal
  • Truncates the BigDecimal to a specified number of decimal places.

    BigDecimal.truncate(BigDecimal('123'), -1);// == BigDecimal('123'))
    BigDecimal.truncate(BigDecimal('-12.3'))// == BigDecimal('-12'))
    BigDecimal.truncate(BigDecimal('12.3'))// == BigDecimal('12'))

zero

  • zero(this): BigDecimal
  • Returns the additive identity element of T, 0.

    Number.zero(); // 0

Scaling

normalize

  • normalize(self): BigDecimal
  • Returns a normalized value

    BigDecimal.normalize(BigDecimal('1.020')); // BigDecimal('1.02')
    BigDecimal.normalize(BigDecimal('1.0200')); // BigDecimal('1.02')

scale

  • scale(self, newScale): BigDecimal
  • Scales a given BigDecimal to the specified scale.

    const value = BigDecimal('1.02');
    BigDecimal.scale(value, 1); // BigDecimal('1.0')
    BigDecimal.scale(value, 3); // BigDecimal('1.020')

Type

__inspect__

__inspect__: Option<(anyValue, depth, options, inspect) => string>

When defined, returns a custom string representation. To be useful, it should be bound to a prototype (ex: Struct)

import { inspect } from 'node:util';

interface Foo {
foo: boolean;
}
const Foo = Struct.define<Foo>({
typeName: 'Foo',
__inspect__: (self) => `Foo { ${String(self.foo)} }`,
});
const myStruct = Struct.create(Foo, { foo: true });// 'Foo { true }'
inspect(myStruct);// 'Foo { true }'

readonlytypeName

typeName: BigDecimal

The factory type constant

asInstance

  • asInstance(this, anyValue): Option<BigDecimal>
  • Try to convert anyValue to enum value or else returns Option.None

    const StringType: Type<string>;
    StringType.asInstance('foo'); // Option.Some('foo')
    StringType.asInstance(12); // Option.None

hasInstance

  • hasInstance(this, anyValue): anyValue is BigDecimal
  • Return true if the given value is an instance of the class.

    const StringType: Type<string>;
    StringType.hasInstance('foo'); // true
    StringType.hasInstance(42); // false

Other

__call__

  • __call__(stringValue): BigDecimal
  • __call__(value, scale): BigDecimal

format

  • format(self): string
  • Returns a string representation of a BigDecimal

    BigDecimal.format(BigDecimal('1.020')); // '1.020'

parse

  • parse(expression): Option<BigDecimal>
  • Returns a new BigDecimal from a string

    BigDecimal.parse('1.020'); // Option.Some(BigDecimal('1.020'))
    BigDecimal.parse('A'); // Option.None