create

@ExperimentalSince(version = KotoolsTypesVersion.V4_5_0)
fun create(number: Number): PositiveInt

Creates a PositiveInt from the specified number, which may involve rounding or truncation, or throws an IllegalArgumentException if the number is less than zero.


Calling from Kotlin

Here's an example of calling this function from Kotlin code:

val isSuccess: Boolean = try {
PositiveInt.create(23)
true
} catch (exception: IllegalArgumentException) {
false
}
assertTrue(isSuccess)

Calling from Java

Here's an example of calling this function from Java code:

boolean isSuccess;
try {
PositiveInt.Companion.create(23);
isSuccess = true;
} catch (final IllegalArgumentException exception) {
isSuccess = false;
}
Assertions.assertTrue(isSuccess);

You can use the PositiveInt.Companion.createOrNull function for returning null instead of throwing an exception in case of invalid number.