Skip to content

Rethink Numbers #15

@f3oall

Description

@f3oall

What problem is this solving

Better Numbers handling

Proposed solution

export type NumberPrefixes = "symbol" | "word" | "name" 
export type Separators = {thousands: string, decimal: string}
export type HumanizedNumber = string

export function toNumber(num: number | string | BigInt, prefixType ?: NumberPrefixes, separators: Separators = toNumber.separators) :  HumanizeNumber {}
toNumber.prototype.separators = {thousands: " ", decimal: "."}

export function fromNumber(num: HumanizedNumber) : number {}
// Example 
const n = toNumber(10000.33) // result: "10 000.33"
const n1 = toNumber(11234.11234, "word") // result "11 thousands 234 point 1 tenths 1 hundredths 2 thousandths 34
const num = fromNumber(n1) // result 11234.11234

export type Precisions = {
  decimal: number,
  integer: number
}
export function toRoundedNumber(num: number | string | BigInt, prefixType ?: NumberPrefixes, precisions: Precision = toRoundedNumber.precision, separators: Separators = toRoundedNumber.separators) :  HumanizeNumber {}
toRoundedNumber.prototype.separators = {thousands: " ", decimal: "."}
toRoundedNumber.prototype.precisions = {integer:0:, decimal: 2}

// Example
const n2 = toRoundedNumber(11234.166) // result: "10 000.17"
const n3 = toRoundedNumber(11244.761, undefined, {integer: 2, decimal: 0}) // result: "11 300"
const n4 = toRoundedNumber(11234.161, "word") // result: "10 thousands point 1 tenths 7 hundrendths"

Describe alternatives you've considered

No response

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions