IntegerConverter

The IntegerConverter class allows you to convert arbitrary values into integers using extended conversion rules.

Description

The IntegerConverter class allows you to convert arbitrary values into integers using the following extended conversion rules:

  • Strings are converted to floats, then to integers
  • DateTime: total number of milliseconds since unix epoсh
  • Boolean: 1 for True and 0 for False

Static methods

toInteger

Converts value into integer or returns 0 when conversion is not possible.
See LongConverter.toLong,
LongConverter.toLongWithDefault

public static toInteger(value: any): number

  • value: any - the value to convert.
  • returns: number - integer value or 0 when conversion is not supported.

toIntegerWithDefault

Converts value into integer or returns default value when conversion is not possible. See LongConverter.toLongWithDefault,
LongConverter.toNullableLong

public static toIntegerWithDefault(value: any, defaultValue: number): number

  • value: any - the value to convert.
  • defaultValue: number - the default value.
  • returns: number - integer value or default when conversion is not supported.

toNullableInteger

Converts value into integer or returns null when conversion is not possible. See LongConverter.toNullableLong

public static toNullableInteger(value: any): number

  • value: any - the value to convert.
  • returns: number - integer value or null when conversion is not supported.

Examples

let value1 = IntegerConverter.toNullableInteger("ABC"); // Result: null
let value2 = IntegerConverter.toNullableInteger("123.456"); // Result: 123
let value3 = IntegerConverter.toNullableInteger(true); // Result: 1
let value4 = IntegerConverter.toNullableInteger(new Date()); // Result: current milliseconds (E.g. 1619869474907)