toNegativeIntOrNull

@ExperimentalSinceKotoolsTypes(version = "4.3.1")
fun Number.toNegativeIntOrNull(): NegativeInt?

Returns this number as a NegativeInt, which may involve rounding or truncation, or returns null if this number is strictly positive.

var result: NegativeInt? = (-1).toNegativeIntOrNull()
println(result) // -1

result = 0.toNegativeIntOrNull()
println(result) // 0

result = 1.toNegativeIntOrNull()
println(result) // null

You can use the toNegativeIntOrThrow function for throwing an IllegalArgumentException instead of returning null when this number is strictly positive.