string-remove-thousand-separators6.0.1

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");

// IT'S SMART TOO:

// 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)
assert.equal(
  remSep("100 000 000 000"),
  "100000000000"
);

// 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");
console.log();
// ^ Western notation

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

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

API

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

Defaults:

    {
removeThousandSeparatorsFromNumbers: true,
padSingleDecimalPlaceNumbers: true,
forceUKStyle: false
}
options object's key Type Obligatory? Default Description
removeThousandSeparatorsFromNumbers Boolean no true Should remove thousand separators? 1,000,000 β†’ 1000000? Or Swiss-style, 1'000'000 β†’ 1000000? Or Russian-style, 1 000 000 β†’ 1000000?
padSingleDecimalPlaceNumbers Boolean no true Should we pad one decimal place numbers with zero? 100.2 β†’ 100.20?
forceUKStyle Boolean no false Should we convert the decimal separator commas into dots? 1,5 β†’ 1.5?

Changelog

See it in the monorepo opens in a new tab, on GitHub.

Contributing

To report bugs or request features or assistance, raise an issue on GitHub opens in a new tab.

Any code contributions welcome! All Pull Requests will be dealt promptly.

Licence

MIT opens in a new tab

Copyright Β© 2010–2021 Roy Revelt and other contributors

Related packages:

πŸ“¦ detergent 8.0.1
Extracts, cleans and encodes text
πŸ“¦ csv-split-easy 6.0.1
Splits the CSV string into array of arrays, each representing a row of columns
πŸ“¦ string-remove-widows 3.0.1
Helps to prevent widow words in a text
πŸ“¦ string-character-is-astral-surrogate 2.0.1
Tells, is given character a part of astral character, specifically, a high and low surrogate
πŸ“¦ string-unfancy 5.0.1
Replace all n/m dashes, curly quotes with their simpler equivalents
πŸ“¦ string-trim-spaces-only 4.0.1
Like String.trim() but you can choose granularly what to trim
πŸ“¦ string-collapse-leading-whitespace 6.0.1
Collapse the leading and trailing whitespace of a string