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.
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.
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.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[null,null,["Last updated 2025-02-10 UTC."],[],[],null,["# MathUtils\n\nadded in [version 26.1.0](/topic/libraries/support-library/revisions) \nbelongs to Maven artifact com.android.support:support-compat:28.0.0-alpha1 \nSummary: [Methods](#pubmethods) \\| [Inherited Methods](#inhmethods) \\| [\\[Expand All\\]](#) \n\nMathUtils\n=========\n\n| This package is part of the Android [support library](/topic/libraries/support-library) which is no longer maintained. The support library has been superseded by [AndroidX](/jetpack/androidx) which is part of [Jetpack](/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider [migrating](/jetpack/androidx/migrate) existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library [class\n| mappings](/jetpack/androidx/migrate/class-mappings).\n\n\n`\npublic\n\n\nclass\nMathUtils\n`\n\n\n`\n\nextends Object\n\n\n`\n\n`\n\n\n`\n\n|---|-----------------------------------|\n| java.lang.Object ||\n| ↳ | android.support.v4.math.MathUtils |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nA utility class providing functions useful for common mathematical operations.\n\nSummary\n-------\n\n| ### Public methods ||\n|------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` static double` | ` `[clamp](/reference/android/support/v4/math/MathUtils#clamp(double, double, double))`(double value, double min, double max) ` This method takes a numerical value and ensures it fits in a given numerical range. |\n| ` static float` | ` `[clamp](/reference/android/support/v4/math/MathUtils#clamp(float, float, float))`(float value, float min, float max) ` This method takes a numerical value and ensures it fits in a given numerical range. |\n| ` static int` | ` `[clamp](/reference/android/support/v4/math/MathUtils#clamp(int, int, int))`(int value, int min, int max) ` This method takes a numerical value and ensures it fits in a given numerical range. |\n\n| ### Inherited methods |\n|-----------------------|---|\n| From class ` java.lang.Object ` |-------------------|-------------------------------| | ` Object` | ` clone() ` | | ` boolean` | ` equals(Object arg0) ` | | ` void` | ` finalize() ` | | ` final Class\u003c?\u003e` | ` getClass() ` | | ` int` | ` hashCode() ` | | ` final void` | ` notify() ` | | ` final void` | ` notifyAll() ` | | ` String` | ` toString() ` | | ` final void` | ` wait(long arg0, int arg1) ` | | ` final void` | ` wait(long arg0) ` | | ` final void` | ` wait() ` | ||\n\nPublic methods\n--------------\n\n### clamp\n\nadded in [version 26.1.0](/topic/libraries/support-library/revisions) \n\n```\ndouble clamp (double value, \n double min, \n double max)\n```\n\nThis method takes a numerical value and ensures it fits in a given numerical range. If the\nnumber is smaller than the minimum required by the range, then the minimum of the range will\nbe returned. If the number is higher than the maximum allowed by the range then the maximum\nof the range will be returned.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|---------|-------------------------------------------|\n| `value` | `double`: the value to be clamped. \u003cbr /\u003e |\n| `min` | `double`: minimum resulting value. \u003cbr /\u003e |\n| `max` | `double`: maximum resulting value. \u003cbr /\u003e |\n\n| Returns ||\n|----------|---------------------------|\n| `double` | the clamped value. \u003cbr /\u003e |\n\n### clamp\n\nadded in [version 26.1.0](/topic/libraries/support-library/revisions) \n\n```\nfloat clamp (float value, \n float min, \n float max)\n```\n\nThis method takes a numerical value and ensures it fits in a given numerical range. If the\nnumber is smaller than the minimum required by the range, then the minimum of the range will\nbe returned. If the number is higher than the maximum allowed by the range then the maximum\nof the range will be returned.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|---------|------------------------------------------|\n| `value` | `float`: the value to be clamped. \u003cbr /\u003e |\n| `min` | `float`: minimum resulting value. \u003cbr /\u003e |\n| `max` | `float`: maximum resulting value. \u003cbr /\u003e |\n\n| Returns ||\n|---------|---------------------------|\n| `float` | the clamped value. \u003cbr /\u003e |\n\n### clamp\n\nadded in [version 26.1.0](/topic/libraries/support-library/revisions) \n\n```\nint clamp (int value, \n int min, \n int max)\n```\n\nThis method takes a numerical value and ensures it fits in a given numerical range. If the\nnumber is smaller than the minimum required by the range, then the minimum of the range will\nbe returned. If the number is higher than the maximum allowed by the range then the maximum\nof the range will be returned.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|---------|----------------------------------------|\n| `value` | `int`: the value to be clamped. \u003cbr /\u003e |\n| `min` | `int`: minimum resulting value. \u003cbr /\u003e |\n| `max` | `int`: maximum resulting value. \u003cbr /\u003e |\n\n| Returns ||\n|-------|---------------------------|\n| `int` | the clamped value. \u003cbr /\u003e |\n\n-\n\n Classes\n -------\n\n - [MathUtils](/reference/android/support/v4/math/MathUtils)"]]