commas — thousands-separator add/remove
import { addCommas, removeCommas } from "@persian-tools/persian-tools";
// CommonJS
const { addCommas, removeCommas } = require("@persian-tools/persian-tools");
Public exports
addCommas(input: number | string): string
removeCommas(value: string): number
addCommas
Inserts commas every three digits, working on the integer part and preserving any decimal portion.
import { addCommas } from "@persian-tools/persian-tools";
addCommas(30000); // "30,000"
addCommas("1234567.89"); // "1,234,567.89"
addCommas(-2500); // "-2,500"
addCommas("۱۲۳۴۵"); // "12,345" (Persian digits auto-converted to English first)
Behaviour rules
- Accepts
number | string. Anything else (null,undefined,boolean,object) returns""(no throw). - Strings are first stripped of any pre-existing commas (
input.replace(/,/g, "")). - If the input string is detected as Persian (
isPersian(...)) the digits are converted viadigitsFaToEnbefore formatting. - The cleaned string must match
/^-?\d+(\.\d+)?$/. Anything else (letters, exponent notation, non-Persian non-Latin digits) returns"".
Pitfalls
- Arabic-Indic digits (
٠-٩) are NOT handled.isPersian("٧٨")is false, so the Arabic digits fall through and fail the final regex →"". Normalize first withdigitsArToEnif input may come from Arabic keyboards. addCommas(NaN)→"", not"NaN"(NaN's stringification fails the regex).addCommas(1e21)→"". JavaScript'sNumber.prototype.toStringswitches to scientific notation around 1e21, which the regex rejects. For huge numbers, format as a string first.
removeCommas
Strips commas (and any whitespace after each comma) and parses the result with Number(...).
import { removeCommas } from "@persian-tools/persian-tools";
removeCommas("1,234,567"); // 1234567
removeCommas("30,000.5"); // 30000.5
removeCommas("-2,500"); // -2500
Behaviour rules
- Input must be a string. Otherwise throws:
TypeError("PersianTools: removeCommas - The input must be string"). - Empty string returns
0(sinceNumber("")is0). - Non-numeric content like
"abc"returnsNaN— the caller is responsible for checking withNumber.isNaN(...). - Does not convert Persian/Arabic digits.
removeCommas("۱۲۳")returnsNaN. Run digit normalization first if needed.
Pitfalls
removeCommas("1,234,567")returns anumber, not astring. For round-tripping (string → number → string) compose withaddCommas.- No throw for malformed numbers —
removeCommas("12abc")returnsNaN. Validate before use.
Common patterns
Sanitize → format pipeline for user input
import { addCommas, autoConvertDigitsToEN } from "@persian-tools/persian-tools";
const formatPrice = (raw: string) => addCommas(autoConvertDigitsToEN(raw));
formatPrice("۱۲۳۴۵"); // "12,345"
formatPrice("٧٨٩"); // "789" (handles both Persian and Arabic digits)
Parse a displayed price back to a number
import { removeCommas, autoConvertDigitsToEN } from "@persian-tools/persian-tools";
const parsePrice = (display: string) => removeCommas(autoConvertDigitsToEN(display));
parsePrice("۱۲,۳۴۵"); // 12345
References
- Tests:
test/addCommas.spec.ts,test/removeCommas.spec.ts - Related:
digitsskill for the digit conversion functions