lang.int
Module lang.int
Declarations
ballerina/lang.int Ballerina library
Module overview
The lang.int
module corresponds to the int
basic type.
Functions
abs
Returns the absolute value of an int value.
int n = -25; n.abs() ⇒ 25 int:abs(-30) ⇒ 30
Parameters
- n int - int value to be operated on
Return Type
- int - absolute value of parameter
n
avg
Returns the average of its arguments.
int:avg(10, 20, 30, 40) ⇒ 25.0
Return Type
- decimal - average of parameter
n
and all of parameterns
fromHexString
Returns the integer that a string value represents in hexadecimal.
Both uppercase A-F and lowercase a-f are allowed.
It may start with an optional +
or -
sign.
No 0x
or 0X
prefix is allowed.
Returns an error if the parameter s
is not in an allowed format.
int:fromHexString("1A5F") ⇒ 6751 int:fromHexString("-2b4a") ⇒ -11082 int:fromHexString("1Y4K") ⇒ error
Parameters
- s string - hexadecimal string representation of int value
fromString
Returns the integer of a string that represents in decimal.
Returns error if parameter s
is not the decimal representation of an integer.
The first character may be +
or -
.
This is the inverse of function value:toString
applied to an int
.
int:fromString("76") ⇒ 76 int:fromString("-120") ⇒ -120 int:fromString("0xFFFF") ⇒ error
Parameters
- s string - string representation of a integer value
max
Returns the maximum of one or more int values.
int:max(50, 20, 30, 70, 65) ⇒ 70 [int, int, int] scores = [52, 95, 76]; int:max(...scores) ⇒ 95 int n = 18; n.max(25, 30, 4, 15) ⇒ 30
Return Type
- int - maximum value of value of parameter
n
and all of parameterns
min
Returns the minimum of one or more int values.
int:min(45, 25, 30, 75, 50) ⇒ 25 [int, int, int, int] points = [21, 12, 48, 14]; int:min(...points) ⇒ 12 int m = 23; m.min(12, 43, 7, 19) ⇒ 7
Return Type
- int - minimum value of parameter
n
and all of parameterns
range
function range(int rangeStart, int rangeEnd, int step) returns object {
*object:Iterable;
public isolated function iterator() returns object {
public isolated function next() returns record {|
int value;
|}?;
};
}
Returns an iterable object that iterates over a range of integers.
The integers returned by the iterator belong to the set S,
where S is { rangeStart + step*i such that i >= 0 }
.
When step > 0
, the members of S that are < rangeEnd
are returned in increasing order.
When step < 0
, the members of S that are > rangeEnd
are returned in decreasing order.
When step = 0
, the function panics.
Parameters
- rangeStart int - the first integer to be returned by the iterator
- rangeEnd int - the exclusive limit on the integers returned by the iterator
- step int - the difference between successive integers returned by the iterator; a positive value gives an increasing sequence; a negative value gives a decreasing sequence
Return Type
- object { *object:Iterable; public isolated function iterator() returns object { public isolated function next() returns record {| int value; |}?; }; } - an iterable object
sum
Returns sum of zero or more int values.
int:sum(10, 20, 30, 40) ⇒ 100 int[] marks = [50, 65, 78, 95]; int:sum(...marks) ⇒ 288 int num = 24; num.sum(38, 15, 97, 27) ⇒ 201
Parameters
- ns int... - int values to sum
Return Type
- int - sum of all of parameter
ns
; 0 if parameterns
is empty
toHexString
Returns representation of an integer as hexdecimal string.
There is no 0x
prefix. Lowercase letters a-f are used.
Negative numbers will have a -
prefix. No sign for
non-negative numbers.
26.toHexString() ⇒ 1a int:toHexString(-158) ⇒ -9e
Parameters
- n int - int value
Return Type
- string - hexadecimal string representation of int value
Constants
lang.int: MAX_VALUE
Maximum value of type int
.
lang.int: MIN_VALUE
Minimum value of type int
.
lang.int: SIGNED16_MAX_VALUE
Maximum value of type Signed16
.
lang.int: SIGNED16_MIN_VALUE
Minimum value of type Signed16
.
lang.int: SIGNED32_MAX_VALUE
Maximum value of type Signed32
.
lang.int: SIGNED32_MIN_VALUE
Minimum value of type Signed32
.
lang.int: SIGNED8_MAX_VALUE
Maximum value of type Signed8
.
lang.int: SIGNED8_MIN_VALUE
Minimum value of type Signed8
.
lang.int: UNSIGNED16_MAX_VALUE
Maximum value of type Unsigned16
.
lang.int: UNSIGNED32_MAX_VALUE
Maximum value of type Unsigned32
.
lang.int: UNSIGNED8_MAX_VALUE
Maximum value of type Unsigned8
.
Integer types
lang.int: Signed32
Signed32
Built-in subtype that allows signed integers that can be represented in 32 bits using two's complement. This allows an int between -2^31 and 2^31 - 1 inclusive, i.e., between -2,147,483,648 and 2,147,483,647 inclusive.
lang.int: Signed16
Signed16
Built-in subtype that allows non-negative integers that can be represented in 16 bits using two's complement. This allows an int between -2^15 and 2^15 - 1 inclusive, i.e., between -32,768 and 32,767 inclusive.
lang.int: Signed8
Signed8
Built-in subtype that allows non-negative integers that can be represented in 8 bits using two's complement. This allows an int between -2^7 and 2^7 - 1 inclusive, i.e., between -128 and 127 inclusive.
lang.int: Unsigned32
Unsigned32
Built-in subtype that allows non-negative integers that can be represented in 32 bits. This allows an int between 0 and 2^32 - 1 inclusive, i.e., between 0 and 4,294,967,295 inclusive.
lang.int: Unsigned16
Unsigned16
Built-in subtype that allows non-negative integers that can be represented in 16 bits. This allows an int between 0 and 2^16 - 1 inclusive, i.e., between 0 and 65,535 inclusive.
lang.int: Unsigned8
Unsigned8
Built-in subtype that allows non-negative integers that can be represented in 8 bits.
This allows an int between 0 and 2^8 - 1 inclusive,
i.e., between 0 and 255 inclusive.
This is the same as byte
.