Methods
abbreviate(n, optionsopt) → {string}
- Description:
- Abbreviates large numbers using standard metric-style suffixes (e.g., `k`, `m`, `b`, `t`).
Useful for displaying large values in a compact and readable form, such as for
social media stats, monetary values, or data counts.
- Source:
Examples
abbreviate(1500)
// Returns: "1.5k"
abbreviate(2500000, { d: 2 })
// Returns: "2.50m"
abbreviate(987654321)
// Returns: "987.7m"
abbreviate(1000000000000)
// Returns: "1t"
Parameters:
Name |
Type |
Attributes |
Description |
n |
number
|
|
The number to abbreviate. |
options |
Object
|
<optional>
|
Optional formatting settings.
Properties
Name |
Type |
Attributes |
Default |
Description |
d |
number
|
<optional>
|
1
|
Number of decimal places to include in the abbreviated output. |
|
Returns:
A compact, human-readable representation of the number.
-
Type
-
string
convertBase(n, to, fromopt) → {string}
- Description:
- Converts a number from one base to another.
Supports bases up to 62, using digits, uppercase letters, and lowercase letters as symbols.
Throws an error if the number includes invalid characters for the given base.
- Source:
Examples
convertBase("1010", 10, 2)
// Returns: "10"
convertBase(255, 16)
// Returns: "FF"
convertBase("FF", 10, 16)
// Returns: "255"
convertBase("100", 36, 10)
// Returns: "2S"
Parameters:
Name |
Type |
Attributes |
Default |
Description |
n |
number
|
string
|
|
|
The number to convert. Can be provided as a numeric value or string. |
to |
number
|
|
|
The target base to convert to (e.g., 2 for binary, 16 for hexadecimal). |
from |
number
|
<optional>
|
10
|
The original base of the input number. Defaults to base 10. |
Throws:
-
Throws an error if the base exceeds 62 or if the number contains invalid digits.
-
-
Type
-
Error
Returns:
The number represented in the target base.
-
Type
-
string
relativeTime(date, optionsopt) → {string}
- Description:
- Converts a date into a human-readable relative time string.
This function compares a given date to the current time (or a custom reference date)
and expresses the difference in natural language — for example, `"3 days ago"` or `"in 2 hours"`.
It supports multiple levels of detail (e.g., `"1 year, 2 months"`), abbreviations, and optional suffixes.
- Source:
Examples
relativeTime(new Date(Date.now() - 60000))
// Returns: "1 minute ago"
relativeTime(new Date(Date.now() + 3600000))
// Returns: "in 1 hour"
relativeTime(new Date(Date.now() - 90061000), { lod: 2 })
// Returns: "1 day, 1 hour ago"
relativeTime(new Date(Date.now() - 90061000), { abbreviate: true, includeSuffix: false })
// Returns: "1d, 1h"
Parameters:
Name |
Type |
Attributes |
Description |
date |
Date
|
|
The target date to compare against the reference date. |
options |
Object
|
<optional>
|
Optional settings to control formatting and precision.
Properties
Name |
Type |
Attributes |
Default |
Description |
now |
Date
|
<optional>
|
new Date()
|
The reference date to compare with (defaults to the current time). |
lod |
number
|
<optional>
|
1
|
Level of detail (number of time units to include).
For example, `2` might produce `"1 year, 2 months"` instead of just `"1 year"`. |
separator |
string
|
<optional>
|
", "
|
String used to separate time units in the output. |
abbreviate |
boolean
|
<optional>
|
false
|
Whether to abbreviate units (e.g., `"1d"` instead of `"1 day"`). |
includeSuffix |
boolean
|
<optional>
|
true
|
Whether to include `"ago"` or `"in"` to indicate direction in time. |
|
Returns:
A human-readable string representing the relative time.
-
Type
-
string
separate(n, optionsopt) → {string}
- Description:
- Inserts a separator character into a number for improved readability.
This function groups digits in the integer part of a number into segments
(such as thousands) and optionally preserves decimal parts. Useful for
formatting large numbers like `1000000` into `1,000,000`.
- Source:
Examples
separate(1234567)
// Returns: "1,234,567"
separate(1234567.89)
// Returns: "1,234,567.89"
separate(987654321, { char: " ", segment: 3 })
// Returns: "987 654 321"
separate(123456, { segment: 2, char: "_" })
// Returns: "1_23_45_6"
Parameters:
Name |
Type |
Attributes |
Description |
n |
number
|
|
The number to format. |
options |
Object
|
<optional>
|
Optional formatting settings.
Properties
Name |
Type |
Attributes |
Default |
Description |
char |
string
|
<optional>
|
","
|
The character used to separate digit groups. |
segment |
number
|
<optional>
|
3
|
The number of digits per group. |
|
Returns:
The formatted number string with separators applied.
-
Type
-
string
spell(n, optionsopt) → {string}
- Description:
- Converts a number into its English words representation.
This function handles both integer and decimal parts of the number.
It supports optional formatting for hyphens between tens and ones,
inclusion of "and" in hundreds, and a custom separator between words.
- Source:
Examples
spell(123)
// Returns: "one hundred twenty three"
spell(123, { and: true })
// Returns: "one hundred and twenty three"
spell(45.67, { hyphens: true })
// Returns: "forty-five point six seven"
spell(1001, { separator: "-" })
// Returns: "one-thousand-one"
Parameters:
Name |
Type |
Attributes |
Description |
n |
number
|
|
The number to convert. Can be an integer or float. |
options |
Object
|
<optional>
|
Optional settings for formatting.
Properties
Name |
Type |
Attributes |
Default |
Description |
hyphens |
boolean
|
<optional>
|
false
|
Whether to include hyphens between tens and ones (e.g., "twenty-one"). |
and |
boolean
|
<optional>
|
false
|
Whether to include "and" after hundreds (e.g., "one hundred and twenty"). |
separator |
string
|
<optional>
|
" "
|
The string used to separate words in the output. |
|
Returns:
The English words representation of the number.
-
Type
-
string
toFraction(n, optionsopt) → {string}
- Description:
- Converts a decimal number into its fractional representation.
Supports mixed fractions, typographic fraction symbols, and custom spacing.
Simplifies the resulting fraction using the greatest common factor (GCF).
- Source:
Examples
toFraction(0.75)
// Returns: "3/4"
toFraction(1.25, { mixed: true })
// Returns: "1 1/4"
toFraction(2.5, { pretty: true })
// Returns: "5⁄2"
toFraction(1.333, { mixed: true, symbol: " / ", space: " " })
// Returns: "1 1 / 3"
Parameters:
Name |
Type |
Attributes |
Description |
n |
number
|
|
The decimal number to convert. |
options |
Object
|
<optional>
|
Optional formatting settings.
Properties
Name |
Type |
Attributes |
Default |
Description |
mixed |
boolean
|
<optional>
|
false
|
Whether to display improper fractions as mixed numbers (e.g., `1 1/2` instead of `3/2`). |
pretty |
boolean
|
<optional>
|
false
|
Whether to use a typographic fraction slash (`⁄`) instead of a standard slash (`/`). |
symbol |
string
|
<optional>
|
pretty ? "⁄" : "/"
|
The symbol used to separate numerator and denominator. |
space |
string
|
<optional>
|
" "
|
The string used between whole and fractional parts in mixed numbers. |
|
Returns:
The simplified fractional representation of the input number.
-
Type
-
string