number-string
v2.0.1
Published
Functions to format number to string
Downloads
475
Readme
Usage
.toNumber
Convert a string to number disregarding other characters
import { toNumber } from "number-string";
toNumber("$1.57"); // 1.57
Parameters:
toNumber(stringOrNumber, {
decimalMark = ".",
});
.toNumberString
Convert a string to string of the numbers disregarding other characters
import { toNumberString } from "number-string";
toNumberString("$1.57"); // "1.57"
Parameters:
toNumberString(stringOrNumber, {
decimalMark = ".",
});
.toClean
Like toFixed but removes trailing 0's
import { toClean } from "number-string";
toClean(1.5009, {maxPrecision: 2}); // "1.5"
Parameters:
toClean(stringOrNumber, {
decimalMark = ".",
thousandSeparator = ",",
maxPrecision = 10, // maximum precision possible is 10 to prevent floating point errors
minPrecision = 0,
});
.toMoney
Converts number to currency
import { toMoney } from "number-string";
toMoney(-1234.5); // "($1,234.50)"
Parameters:
toMoney(stringOrNumber, {
decimalMark = ".",
thousandSeparator = ",",
maxPrecision = 2, // maximum precision possible is 10 to prevent floating point errors
minPrecision = 2,
symbol = "$",
symbolBehind = false,
useParens = true,
});
.toClosest
Rounds to the closest interval
import { toClosest } from "number-string";
toClosest(12.6, 7.1); // 14.2
Parameters:
toClosest(stringOrNumber, roundToNearestNumber);