/to-number

Simple utility to convert number-like variables to a number

Primary LanguageJavaScript

to-number

Simple utility to get a number from number-like variables. If it can't convert to a number it returns false.

toNumber

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

toInt

Signature:

toInt(numberLikeVariable, options);

Options:

  • separator: The number separator (defaults to ,)

Example:

toInt('23,490.82', { separator: ',' }); // 23490

toFloat

Signature:

toFloat(numberLikeVariable, options);

Options:

  • separator: The number separator (defaults to ,)
  • decimalPlaces: The number separator (defaults to null)

Example:

toFloat('23,490.8223', { separator: ',' }); // 23490.8223
toFloat('23,490.8223', { decimalPlaces: 2 }); // 23490.82
toFloat('23,490.8223', { decimalPlaces: 0 }); // 23490

toPositiveInt

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 to false)

Example:

toPositiveInt('23,490.8223', { separator: ',' }); // 23490
toPositiveInt(0, { includeZero: true }); // 0
toPositiveInt(0); // false
toPositiveInt(-10); // false

toPositiveFloat

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 to false)
  • decimalPlaces: The number separator (defaults to null)

Example:

toPositiveFloat('23,490.8223'); // 23490.8223
toPositiveFloat(0, { includeZero: true }); // 0
toPositiveFloat(0); // false
toPositiveFloat(-10); // false

toNegativeInt

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 to false)

Example:

toNegativeInt('-23,490.8223', { separator: ',' }); // -23490
toNegativeInt(0, { includeZero: true }); // 0
toNegativeInt(0); // false
toNegativeInt(10); // false

toNegativeFloat

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 to false)
  • decimalPlaces: The number separator (defaults to null)

Example:

toNegativeFloat('-23,490.8223'); // -23490.8223
toNegativeFloat(0, { includeZero: true }); // 0
toNegativeFloat(0); // false
toNegativeFloat(-10); // -10