Detects and removes thousand separators (dot/comma/quote/space) from string-type digits

Β§ Quick Take

import { strict as assert } from "assert";
import remSep from "string-remove-thousand-separators";

// πŸ‡¬πŸ‡§ πŸ‡ΊπŸ‡Έ thousand separators:
assert.equal(remSep("1,000,000.00"), "1000000.00");

// πŸ‡·πŸ‡Ί  thousand separators:
assert.equal(remSep("1 000 000,00"), "1000000,00");
// (if you want it converted to Western notation with dot,
// set opts.forceUKStyle = true

// πŸ‡¨πŸ‡­ thousand separators:
assert.equal(remSep("1'000'000.00"), "1000000.00");


// will not delete if the thousand separators are mixed:
const input = "100,000,000.000";
assert.equal(remSep(input), input);
// ^ does nothing

// but will remove empty space, even if there is no decimal separator:
// (that's to cope with Russian notation integers that use thousand separators)
  remSep("100 000 000 000"),

// while removing thousand separators, it will also pad the digits to two decimal places
// (optional, on by default, to turn it off set opts.padSingleDecimalPlaceNumbers to `false`):
assert.equal(remSep("100,000.2"), "100000.20");
// ^ Western notation

assert.equal(remSep("100 000,2"), "100000,20");
// ^ Russian notation

assert.equal(remSep("100'000.2"), "100000.20");
// ^ Swiss notation


remSep(str, [opts])

If first argument (input) is not string, it will throw and error. Second input argument, opts, is optional. However, if it is present and is not null, not undefined and not a plain object, it will throw and error.

Β§ options


removeThousandSeparatorsFromNumbers: true,
padSingleDecimalPlaceNumbers: true,
forceUKStyle: false
options object's keyTypeObligatory?DefaultDescription
removeThousandSeparatorsFromNumbersBooleannotrueShould remove thousand separators? 1,000,000 β†’ 1000000? Or Swiss-style, 1'000'000 β†’ 1000000? Or Russian-style, 1 000 000 β†’ 1000000?
padSingleDecimalPlaceNumbersBooleannotrueShould we pad one decimal place numbers with zero? 100.2 β†’ 100.20?
forceUKStyleBooleannofalseShould we convert the decimal separator commas into dots? 1,5 β†’ 1.5?

Β§ Licence

MIT opens in a new tab

Copyright Β© 2010–2020 Roy Revelt and other contributors

Related packages:

πŸ“¦ detergent 5.11.10
Extracts, cleans and encodes text
πŸ“¦ csv-split-easy 3.1.0
Splits the CSV string into array of arrays, each representing a row of columns
πŸ“¦ string-fix-broken-named-entities 3.0.11
Finds and fixes common and not so common broken named HTML entities, returns ranges array of fixes
πŸ“¦ string-range-expander 1.11.11
Expands string index ranges within whitespace boundaries until letters are met
πŸ“¦ string-process-comma-separated 1.2.14
Extracts chunks from possibly comma or whatever-separated string
πŸ“¦ string-left-right 2.3.31
Looks up the first non-whitespace character to the left/right of a given index
πŸ“¦ string-convert-indexes 2.0.2
Convert between native JS string character indexes and grapheme-count-based indexes