Simple utility to get a number from number-like variables. If it can't convert to a number it returns false
.
Converts number-like variable to a number. If it can't convert the number it returns false
.
Signature:
toNumber(numberLikeVariable, options);
Options:
separator
: The number separator (defaults to,
)
Example:
toNumber('23,490.82', { separator: ',' }); // 23490.82
toNumber('23 490.82', { separator: ' ' }); // 23490.82
Signature:
toInt(numberLikeVariable, options);
Options:
separator
: The number separator (defaults to,
)
Example:
toInt('23,490.82', { separator: ',' }); // 23490
Signature:
toFloat(numberLikeVariable, options);
Options:
separator
: The number separator (defaults to,
)decimalPlaces
: The number separator (defaults tonull
)
Example:
toFloat('23,490.8223', { separator: ',' }); // 23490.8223
toFloat('23,490.8223', { decimalPlaces: 2 }); // 23490.82
toFloat('23,490.8223', { decimalPlaces: 0 }); // 23490
Signature:
toPositiveInt(numberLikeVariable, options);
Options:
separator
: The number separator (defaults to,
)includeZero
: Since zero is neither positive nor negative, use this to include if needed (defaults tofalse
)
Example:
toPositiveInt('23,490.8223', { separator: ',' }); // 23490
toPositiveInt(0, { includeZero: true }); // 0
toPositiveInt(0); // false
toPositiveInt(-10); // false
Signature:
toPositiveFloat(numberLikeVariable, options);
Options:
separator
: The number separator (defaults to,
)includeZero
: Since zero is neither positive nor negative, use this to include if needed (defaults tofalse
)decimalPlaces
: The number separator (defaults tonull
)
Example:
toPositiveFloat('23,490.8223'); // 23490.8223
toPositiveFloat(0, { includeZero: true }); // 0
toPositiveFloat(0); // false
toPositiveFloat(-10); // false
Signature:
toNegativeInt(numberLikeVariable, options);
Options:
separator
: The number separator (defaults to,
)includeZero
: Since zero is neither positive nor negative, use this to include if needed (defaults tofalse
)
Example:
toNegativeInt('-23,490.8223', { separator: ',' }); // -23490
toNegativeInt(0, { includeZero: true }); // 0
toNegativeInt(0); // false
toNegativeInt(10); // false
Signature:
toNegativeFloat(numberLikeVariable, options);
Options:
separator
: The number separator (defaults to,
)includeZero
: Since zero is neither positive nor negative, use this to include if needed (defaults tofalse
)decimalPlaces
: The number separator (defaults tonull
)
Example:
toNegativeFloat('-23,490.8223'); // -23490.8223
toNegativeFloat(0, { includeZero: true }); // 0
toNegativeFloat(0); // false
toNegativeFloat(-10); // -10