Installation
1. Install the latest version with yarn:
yarn add string-remove-thousand-separators
or with npm:
npm install string-remove-thousand-separators
2. Import it in your code:
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()
The main function remSep()
is imported like this:
import { remSep } from "string-remove-thousand-separators";
Itβs a function which takes three input arguments:
function remSep (str: string, opts?: Partial<Opts>): string;
Input argument | Type | Obligatory | Description |
---|---|---|---|
str Type: String Obligatory: yes | |||
str | String | yes | Input to work upon. |
opts Type: Plain object Obligatory: no | |||
opts | Plain object | no | Optional Options Object. |
None of the input arguments will be mutated by this program, we have unit tests to prove that.
The Optional Options Object has the following shape:
{
removeThousandSeparatorsFromNumbers: boolean;
padSingleDecimalPlaceNumbers: boolean;
forceUKStyle: boolean;
}
Key | Type | Default | Description |
---|---|---|---|
removeThousandSeparatorsFromNumbers Type: Boolean Default: true | |||
removeThousandSeparatorsFromNumbers | Boolean | true | Should remove thousand separators? 1,000,000 β 1000000 ? Or Swiss-style, 1'000'000 β 1000000 ? Or Russian-style, 1 000 000 β 1000000 ? |
padSingleDecimalPlaceNumbers Type: Boolean Default: true | |||
padSingleDecimalPlaceNumbers | Boolean | true | Should we pad one decimal place numbers with zero? 100.2 β 100.20 ? |
forceUKStyle Type: Boolean Default: false | |||
forceUKStyle | Boolean | false | Should we convert the decimal separator commas into dots? 1,5 β 1.5 ? |
Here are all defaults in one place for copying:
{
"removeThousandSeparatorsFromNumbers": true,
"padSingleDecimalPlaceNumbers": true,
"forceUKStyle": false
}
The function will return a string, the input with thousand separators removed.
API β defaults
You can import defaults
:
import { defaults } from "string-remove-thousand-separators";
console.log(JSON.stringify(defaults, null, 4));
It's a plain object:
{
"removeThousandSeparatorsFromNumbers": true,
"padSingleDecimalPlaceNumbers": true,
"forceUKStyle": false
}
The main function calculates the options to be used by merging the options you passed with these defaults.
API β version
You can import version
:
import { version } from "string-remove-thousand-separators";
console.log(version);
// => "7.0.3"