Last active
January 13, 2025 15:29
-
-
Save jessealama/6497868587079a258575a2485f17fb21 to your computer and use it in GitHub Desktop.
A first stab at combining measure, decimal, and normalized decimal data types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export declare class Decimal128 { | |
constructor(value: string | number | bigint | NormalizedDecimal128); | |
// predicates | |
isFinite(): boolean; | |
isNegative(): boolean; | |
isNaN(): boolean; | |
isZero(): boolean; | |
// comparison | |
equals(other: Decimal128): boolean; // mathematical equality | |
lessThan(other: Decimal128): boolean; | |
// serialization | |
toJSON(): string; | |
toString(): string; | |
toFixed(opts?: {digits?: number}): string; | |
toLocaleString(locale?: string): string; | |
toPrecision(opts?: {digits?: number}): string; | |
// no arithmetic | |
// no rounding | |
// conversion | |
toNumber(): number; | |
toBigInt(): bigint; | |
toNormalizedDecimal(): NormalizedDecimal128; // or just "normalize"? | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ExpandedNumber = string // digit strings, not arbitrary strings | |
| number | |
| bigint | |
| Decimal128 | |
| NormalizedDecimal128; | |
export declare class Measure { | |
constructor(value: ExpandedNumber, unit?: string, precision?: number); | |
convertTo(unit: string, precision?: number): Measure; | |
toString(): string; | |
toComponents(): {value: ExpandedNumber, unit: string}[]; | |
getValue(): Decimal128; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export declare class NormalizedDecimal128 { // or just "Decimal"? | |
constructor(x: string | number | bigint | Decimal128); | |
// predicates | |
isFinite(): boolean; | |
isNegative(): boolean; | |
isNaN(): boolean; | |
isZero(): boolean; | |
// comparison | |
equals(other: NormalizedDecimal128): boolean; | |
lessThan(other: NormalizedDecimal128): boolean; | |
// arithmetic | |
abs(): NormalizedDecimal128; | |
add(x: NormalizedDecimal128): NormalizedDecimal128; | |
divide(x: NormalizedDecimal128): NormalizedDecimal128; | |
multiply(x: NormalizedDecimal128): NormalizedDecimal128; | |
negate(): NormalizedDecimal128; | |
subtract(x: NormalizedDecimal128): NormalizedDecimal128; | |
// serlialization | |
toJSON(): string; | |
toString(): string; | |
toFixed(opts?: {digits?: number}): string; | |
toLocaleString(locale?: string): string; | |
toPrecision(opts?: {digits?: number}): string; | |
// rounding | |
round(numFractionalDigits: number, mode: RoundingMode): NormalizedDecimal128; | |
// conversion | |
toDecimal128(): Decimal128; | |
toNumber(): number; | |
toBigInt(): bigint; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the absence of arithmetic support in the Decimal128 class reflects two discussions:
equals
andlessThan
in there, but perhaps those could also be dropped (or maybelessThan
, keepingequals
).