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 int ToInteger(object value)

  • value: object - the value to convert.
  • returns: int - 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 int ToIntegerWithDefault(object value, int defaultValue)

  • value: object - the value to convert.
  • defaultValue: int - the default value.
  • returns: int - 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 int ToNullableInteger(object value)

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

Examples

var value1 = IntegerConverter.ToNullableInteger("ABC"); // Result: null
var value2 = IntegerConverter.ToNullableInteger("123.456"); // Result: 123.456
var value3 = IntegerConverter.ToNullableInteger(true); // Result: 1
var value4 = IntegerConverter.ToNullableInteger(new Date()); // Result: current milliseconds