toPositiveIntOrNull
Returns this number as a PositiveInt, which may involve rounding or truncation, or returns null if this number is strictly negative.
var result: PositiveInt? = 1.toPositiveIntOrNull()
println(result) // 1
result = 0.toPositiveIntOrNull()
println(result) // 0
result = (-1).toPositiveIntOrNull()
println(result) // nullContent copied to clipboard
You can use the toPositiveIntOrThrow function for throwing an IllegalArgumentException instead of returning null when this number is strictly negative.