YUI AS Component Documentation |
Class NumberUtil

Package | com.yahoo.astra.utils
Class | public class NumberUtil

A collection of utility functions for the manipulation of numeric values.

Public Methods

| Method | Defined by
--- | --- | ---
|  | fuzzyEquals(number1:Number, number2:Number, precision:int = 5):Boolean [static] Tests equality for numbers that may have been generated by faulty floating point math. | NumberUtil
|  | roundDownToNearest(number:Number, nearest:Number = 1):Number [static] Rounds a Number down to the nearest multiple of an input. | NumberUtil
|  | roundToNearest(number:Number, nearest:Number = 1):Number [static] Rounds a Number to the nearest multiple of an input. | NumberUtil
|  | roundToPrecision(number:Number, precision:int = 0):Number [static] Rounds a number to a certain level of precision. | NumberUtil
|  | roundUpToNearest(number:Number, nearest:Number = 1):Number [static] Rounds a Number up to the nearest multiple of an input. | NumberUtil

Method detail

fuzzyEquals | () | method

`public static function fuzzyEquals(number1:Number, number2:Number, precision:int = 5):Boolean`

Tests equality for numbers that may have been generated by faulty floating point math. This is not an issue exclusive to the Flash Player, but all modern computing in general. The value is generally offset by an insignificant fraction, and it may be corrected.

Alternatively, this function could be used for other purposes than to correct floating point errors. Certainly, it could determine if two very large numbers are within a certain range of difference. This might be useful for determining "ballpark" estimates or similar statistical analysis that may not need complete accuracy.

Parameters

| `number1:Number` — number1 the first number to test
| `number2:Number` — number2 the second number to test
| `precision:int` (default = `5`)`` — precision the number of digits in the fractional portion to keep

Returns

| `Boolean` — true, if the numbers are close enough to be considered equal, false if not.

roundDownToNearest | () | method |

`public static function roundDownToNearest(number:Number, nearest:Number = 1):Number`

Rounds a Number down to the nearest multiple of an input. For example, by rounding 16 down to the nearest 10, you will receive 10. Similar to the built-in function Math.floor().

Parameters

| `number:Number` — the number to round down
| `nearest:Number` (default = `1`)`` — the number whose mutiple must be found

Returns

| `Number` — the rounded number

See also

Math.floor

roundToNearest | () | method |

`public static function roundToNearest(number:Number, nearest:Number = 1):Number`

Rounds a Number to the nearest multiple of an input. For example, by rounding 16 to the nearest 10, you will receive 20. Similar to the built-in function Math.round().

Parameters

| `number:Number` — the number to round
| `nearest:Number` (default = `1`)`` — the number whose mutiple must be found

Returns

| `Number` — the rounded number

See also

Math.round

roundToPrecision | () | method |

`public static function roundToPrecision(number:Number, precision:int = 0):Number`

Rounds a number to a certain level of precision. Useful for limiting the number of decimal places on a fractional number.

Parameters

| `number:Number` — number the input number to round.
| `precision:int` (default = `0`)`` — precision the number of decimal digits to keep

Returns

| `Number` — the rounded number, or the original input if no rounding is needed

See also

Math.round

roundUpToNearest | () | method |

`public static function roundUpToNearest(number:Number, nearest:Number = 1):Number`

Rounds a Number up to the nearest multiple of an input. For example, by rounding 16 up to the nearest 10, you will receive 20. Similar to the built-in function Math.ceil().

Parameters

| `number:Number` — the number to round up
| `nearest:Number` (default = `1`)`` — the number whose mutiple must be found

Returns

| `Number` — the rounded number

See also

Math.ceil