Skip to main content
The constraints of numbers. The range and sign rules work on every number type, Int, Long, Short, Byte, Double, Float, BigInteger and BigDecimal, with an argument of the same type.
A null argument checks nothing. A rule that reads a value that may be missing, such as max(customer.limit), never fails because of it: required on that value reports it once.
PriceTag.kt
Kotlin’s unsigned types, UInt, ULong, UShort and UByte, have the range constraints too, with arguments written with the u suffix. They cannot be negative, so they have no sign rules:
Packet.kt

Range

min

min(min) on every number type: Int, Long, Short, Byte, Double, Float, BigInteger, BigDecimal, and the unsigned UInt, ULong, UShort, UByte Checks that the number is at least min. min

max

max(max) on every number type: Int, Long, Short, Byte, Double, Float, BigInteger, BigDecimal, and the unsigned UInt, ULong, UShort, UByte Checks that the number is at most max. max

greaterThan

greaterThan(other) on every number type: Int, Long, Short, Byte, Double, Float, BigInteger, BigDecimal, and the unsigned UInt, ULong, UShort, UByte Checks that the number is greater than other. greaterThan

lessThan

lessThan(other) on every number type: Int, Long, Short, Byte, Double, Float, BigInteger, BigDecimal, and the unsigned UInt, ULong, UShort, UByte Checks that the number is less than other. lessThan

between

between(min, max) on every number type: Int, Long, Short, Byte, Double, Float, BigInteger, BigDecimal, and the unsigned UInt, ULong, UShort, UByte Checks that the number is between min and max, both included. between

Sign

positive

positive() on every number type: Int, Long, Short, Byte, Double, Float, BigInteger, BigDecimal Checks that the number is greater than zero. positive

negative

negative() on every number type: Int, Long, Short, Byte, Double, Float, BigInteger, BigDecimal Checks that the number is less than zero. negative

positiveOrZero

positiveOrZero() on every number type: Int, Long, Short, Byte, Double, Float, BigInteger, BigDecimal Checks that the number is zero or greater. positiveOrZero

negativeOrZero

negativeOrZero() on every number type: Int, Long, Short, Byte, Double, Float, BigInteger, BigDecimal Checks that the number is zero or less. negativeOrZero

Whole numbers and multiples

integer

integer() on Double, Float, BigDecimal Checks that the number has no fractional part. Infinity and NaN fail. Trailing zeros do not count, so 2.00 passes. integer

multipleOf

multipleOf(factor) on every number type: Int, Long, Short, Byte, BigInteger, BigDecimal, Double, Float Checks that the number is a multiple of factor. The remainder is computed in decimal, so 0.3 is a multiple of 0.1. Infinity and NaN fail. multipleOf

finite

finite() on Double, Float Checks that the number is neither infinite nor NaN. finite

Digits

minDigits

minDigits(min) on Int, Long, Short, Byte, BigInteger Checks that the number has at least min digits, not counting the sign. minDigits

maxDigits

maxDigits(max) on Int, Long, Short, Byte, BigInteger Checks that the number has at most max digits, not counting the sign. maxDigits

digitCount

digitCount(count) on Int, Long, Short, Byte, BigInteger Checks that the number has exactly count digits, not counting the sign. digitCount

maxIntegerDigits

maxIntegerDigits(max) on BigDecimal Checks that the number has at most max digits before the decimal point. maxIntegerDigits

Decimal places

maxDecimalPlaces

maxDecimalPlaces(max) on BigDecimal, Double, Float Checks that the number has at most max digits after the decimal point. Trailing zeros do not count, so 1.50 has one decimal place. On Double and Float, the number is read in decimal, the way it prints. Infinity and NaN fail. maxDecimalPlaces

decimalPlaces

decimalPlaces(count) on BigDecimal Checks that the number has exactly count digits after the decimal point. Trailing zeros count, so 1.50 has two decimal places, and a whole number has none. decimalPlaces

minDecimalPlaces

minDecimalPlaces(min) on BigDecimal Checks that the number has at least min digits after the decimal point. Trailing zeros count, so 1.50 has two decimal places. minDecimalPlaces
Double and Float are binary, so 0.1 + 0.2 is 0.30000000000000004. multipleOf and maxDecimalPlaces on them read the number in decimal, the way it prints, so 0.3 is a multiple of 0.1. For money, prefer BigDecimal.