No 3rd party dependencies. All dependencies and devDependencies, checked recursively, are Codsen packages.
Permalink to InstallationInstallation
Permalink to Quick TakeQuick Take
Permalink to ExamplesExamples
- Convert decimal commas while retaining grouping separators
- Pad a single decimal digit while removing thousand separators
- Leave a single decimal place unpadded
- Convert a comma decimal separator to a dot
- Remove spaced thousand separators when no decimal separator is present
- Keep thousand separators while still normalising the outer quotes
- Leave a value unchanged when it contains non-numeric text
- Preserve a number whose separators are ambiguous
- Pad one-digit fractions without losing precision
- Remove spaces used as thousand separators
- Remove apostrophes used as thousand separators
API — remSep()
The main function remSep() is imported like this:
The function takes a string and an optional options object:
| Input argument | Type | Obligatory | Description |
|---|---|---|---|
strType: String Obligatory: yes | |||
str | String | yes | Input to work upon. |
optsType: 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:
| Key | Type | Default | Description |
|---|---|---|---|
removeThousandSeparatorsFromNumbersType: 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? |
padSingleDecimalPlaceNumbersType: Boolean Default: true | |||
padSingleDecimalPlaceNumbers | Boolean | true | Pad one decimal place numbers with zero? 100.2 → 100.20? |
forceUKStyleType: Boolean Default: false | |||
forceUKStyle | Boolean | false | Convert decimal commas to dots independently of grouping removal. With default padding, 1,5 → 1.50; 1,50 → 1.50. |
Here are all defaults in one place for copying:
The function returns the formatted value as a string.
Convert decimals while keeping grouping
Set forceUKStyle: true to convert a decimal comma even when removeThousandSeparatorsFromNumbers is false. Grouping characters remain in place:
import { remSep } from "string-remove-thousand-separators";
remSep("1 234,50", {
removeThousandSeparatorsFromNumbers: false,
forceUKStyle: true,
});
// => "1 234.50"
padSingleDecimalPlaceNumbers independently controls whether a single fractional digit gains a trailing zero. It does not change a two-digit fraction.
Pad positive fractions below one
The default padding option also applies to unsigned fractions below one: 0.5 becomes 0.50, and .5 becomes .50. Set padSingleDecimalPlaceNumbers: false to retain a single fractional digit. Existing leading zeros and every fractional digit are preserved; grouping removal does not remove the decimal separator in these fractions.
import { remSep } from "string-remove-thousand-separators";
remSep("0.5"); // => "0.50"
remSep(".5", { padSingleDecimalPlaceNumbers: false }); // => ".5"
remSep("0,075", { forceUKStyle: true }); // => "0.075"
For these recognized fractions, surrounding whitespace and wrapping double quotes are removed before formatting, just as for other recognized numbers. Fractional digits are kept as text, including values too small for JavaScript’s numeric representation.
API — defaults
You can import defaults:
It's a plain object:
The main function calculates the options to be used by merging the options you passed with these defaults.
API — version
You can import version: