Number Functions¶
- number\abs()
- number\ceil()
- number\floor()
- number\format()
- number\parseFloat()
- number\parseInt()
- number\randomInt()
- number\round()
number\abs( )¶
The number\abs() or absolute function is a mathematical function that returns the positive value of a given number, regardless of its sign. In other words, if the input value is negative, the absolute function converts it to its positive counterpart, while if the input value is already positive, it remains unchanged.
For instance, if the input value is -5, the number\abs() function would return 5, whereas if the input value is 7, the function would return 7 as it is already positive. The absolute function is commonly used in programming languages, mathematics, and other scientific applications where it is necessary to obtain the magnitude or distance of a value from zero.
The number\abs() function works with both integers and floating point numbers.
Examples:
$val = number\abs(55.3); // Returns 55.3
$val = number\abs(-55.3); // Returns 55.3
number\ceil( )¶
The number\ceil() function is a mathematical function that rounds a given number up to the next integer.
In its most common usage, the number\ceil() function returns the smallest integer that is greater than or equal to the input value. For example, if the input value is 3.2, the ceil() function would return 4, while if the input value is -4.5, the function would return -4, because -4 is the smallest integer greater than or equal to -4.5.
Examples:
number\floor( )¶
The number\floor() function rounds down a given number to the nearest integer value. This method is commonly used in situations where precise integer values are required, such as in financial calculations, or in situations where a non-integer value must be converted to an integer.
For example, if we apply the number\floor() method to the number 3.8, it will return 3 as the next lowest integer value. Similarly, if we apply the number\floor() method to -2.5, it will return -3 as the next lowest integer value.
Examples:
$val = number\floor(4.5); // Returns 4
$val = number\floor(-4.5); // Returns -5
number\format( )¶
The number\format() function converts numeric VALUE into string formatted according to a specific format or default application settings. If DECIMALS, DECIMAL_MARK OR THOUSAND_SEPARATOR, then system defaults are used.
Examples:
number\format(2.666667, 2); // Returns 2.67
number\format(1000, 2); // Returns 1,000.00
number\format(10.1, 0); // Returns 10.
number\parseInt( )¶
The number\parseInt() function converts a string into an integer.
Examples:
$myInteger = number\parseInt("234"); // Returns 234
$myInteger = number\parseInt("234.32"); // Returns 234
number\parseFloat( )¶
The number\parseFloat() function converts a string representation of a number into a floating-point number.
Example:
number\randomInt( )¶
The number\randomInt() function a cryptographic random integer between specified MIN and MAX. If MIN is omitted, then 0 is used.
The number\randomInt() function takes in two parameters, the minimum and maximum values of the desired range, and then returns a random integer that falls within that range, inclusive of the minimum and maximum values. For example, if the minimum value is 1 and the maximum value is 10, the number\randomInt() function would generate a random integer between 1 and 10, such as 4 or 9.
Example:
number\round( )¶
The number\round() returns the rounded value of VALUE to specified PRECISION (number of digits after the decimal point). PRECISION can also be negative or zero (default).
The number\round() function is a mathematical function that rounds a given value to a specified number of decimal places or to the nearest integer, depending on the precision parameter.
The function takes two parameters, the first is the value that needs to be rounded, and the second is the precision parameter that determines the number of decimal places to which the value should be rounded. If the precision parameter is not specified, the function rounds the value to the nearest integer.
The number\round() function returns an integer or a float value, depending on the input parameters and the type of rounding performed. If the precision parameter is greater than or equal to zero, the function rounds the value to the specified number of decimal places and returns a float value. On the other hand, if the precision parameter is less than zero, the function rounds the value to the nearest integer and returns an integer value.
For example, if the value is 3.14159 and the precision is 2, the number\round() function would return 3.14. If the precision is -1, the function would round the value to the nearest integer and return 3.
Examples:
$val = number\round(3.14159); // Returns 3
$val = number\round(3.14159, 1); // Returns 3.1
$val = number\round(3.14159, 2); // Returns 3.14
$val = number\round(3.14159, 3); // Returns 3.142
$val = number\round(3.14159, 4); // Returns 3.1416
See Also¶
- Array Functions
- Datetime Functions
- Entity Functions
- Env Functions
- Ext Functions
- JSON Functions
- Language Functions
- Number Functions
- Object Functions
- Output Functions
- Password Functions
- Record Functions
- String Functions
- Mythradon Marketing
- Mythradon Sales
- Mythradon Service
- Mythradon System Administration
- Mythradon Tools