Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

MathUtils

open class MathUtils
kotlin.Any
   ↳ androidx.core.math.MathUtils

A utility class providing functions useful for common mathematical operations.

Summary

Public methods

open static Float
clamp(value: Float, min: Float, max: Float)

This method takes a numerical value and ensures it fits in a given numerical range.

open static Double
clamp(value: Double, min: Double, max: Double)

This method takes a numerical value and ensures it fits in a given numerical range.

open static Int
clamp(value: Int, min: Int, max: Int)

This method takes a numerical value and ensures it fits in a given numerical range.

open static Long
clamp(value: Long, min: Long, max: Long)

This method takes a numerical value and ensures it fits in a given numerical range.

Public methods

clamp

open static fun clamp(value: Float, min: Float, max: Float): Float

This method takes a numerical value and ensures it fits in a given numerical range. If the number is smaller than the minimum required by the range, then the minimum of the range will be returned. If the number is higher than the maximum allowed by the range then the maximum of the range will be returned.

Parameters
value Float: the value to be clamped.
min Float: minimum resulting value.
max Float: maximum resulting value.
Return
Float: the clamped value.

clamp

open static fun clamp(value: Double, min: Double, max: Double): Double

This method takes a numerical value and ensures it fits in a given numerical range. If the number is smaller than the minimum required by the range, then the minimum of the range will be returned. If the number is higher than the maximum allowed by the range then the maximum of the range will be returned.

Parameters
value Double: the value to be clamped.
min Double: minimum resulting value.
max Double: maximum resulting value.
Return
Double: the clamped value.

clamp

open static fun clamp(value: Int, min: Int, max: Int): Int

This method takes a numerical value and ensures it fits in a given numerical range. If the number is smaller than the minimum required by the range, then the minimum of the range will be returned. If the number is higher than the maximum allowed by the range then the maximum of the range will be returned.

Parameters
value Int: the value to be clamped.
min Int: minimum resulting value.
max Int: maximum resulting value.
Return
Int: the clamped value.

clamp

open static fun clamp(value: Long, min: Long, max: Long): Long

This method takes a numerical value and ensures it fits in a given numerical range. If the number is smaller than the minimum required by the range, then the minimum of the range will be returned. If the number is higher than the maximum allowed by the range then the maximum of the range will be returned.

Parameters
value Long: the value to be clamped.
min Long: minimum resulting value.
max Long: maximum resulting value.
Return
Long: the clamped value.