fromDecimal
Creates an Integer from the specified text, or throws an IllegalArgumentException if the text doesn't represent an integer.
The text parameter must only contain an optional plus sign (+) or minus sign (-), followed by a sequence of digits (e.g., 1234, +1234, -1234).
In case of invalid text, this function throws an IllegalArgumentException instead of a NumberFormatException to ensure consistent behavior across all Kotlin platforms and to better reflect invalid argument semantics.
Calling from Kotlin
Here's an example of calling this function from Kotlin code:
// Given
val number = 123456L
val text = "$number"
// When
val result: Integer = Integer.fromDecimal(text)
// Then
val expected: Integer = Integer.from(number)
check(result == expected)Content copied to clipboard
Calling from Java
Here's an example of calling this function from Java code:
// Given
final long number = 123456L;
final String text = String.valueOf(number);
// When
final Integer result = Integer.fromDecimal(text);
// Then
final Integer expected = Integer.from(number);
final boolean check = result.equals(expected);
if (!check) throw new IllegalStateException("Check failed.");Content copied to clipboard
See the fromDecimalOrNull function for returning null instead of throwing an exception in case of invalid text.