Int

Functions for interacting with JavaScript Number. See: Number.

bitwiseAnd

RES
let bitwiseAnd: (int, int) => int

bitwiseAnd(n1, n2) calculates the bitwise AND of two integers.

Examples

RES
Int.bitwiseAnd(7, 4) == 4

bitwiseNot

RES
let bitwiseNot: int => int

bitwiseNot(n) calculates the bitwise NOT of an integer.

Examples

RES
Int.bitwiseNot(2) == -3

bitwiseOr

RES
let bitwiseOr: (int, int) => int

bitwiseOr(n1, n2) calculates the bitwise OR of two integers.

Examples

RES
Int.bitwiseOr(7, 4) == 7

bitwiseXor

RES
let bitwiseXor: (int, int) => int

bigwiseXor(n1, n2) calculates the bitwise XOR of two integers.

Examples

RES
Int.bitwiseXor(7, 4) == 3

clamp

RES
let clamp: (~min: int=?, ~max: int=?, int) => int

clamp(~min=?, ~max=?, value) returns value, optionally bounded by min and max.

if max < min returns min.

Examples

RES
Int.clamp(42) == 42 Int.clamp(42, ~min=50) == 50 Int.clamp(42, ~max=40) == 40 Int.clamp(42, ~min=50, ~max=40) == 50

compare

RES
let compare: (int, int) => Ordering.t

equal

RES
let equal: (int, int) => bool

fromFloat

RES
let fromFloat: float => int

fromFloat(n) return an int representing the given value. The conversion is done by truncating the decimal part.

Examples

RES
Int.fromFloat(2.0) == 2 Int.fromFloat(1.999) == 1 Int.fromFloat(1.5) == 1 Int.fromFloat(0.9999) == 0

fromString

RES
let fromString: (string, ~radix: int=?) => option<int>

fromString(str, ~radix=?) return an option<int> representing the given value str. ~radix specifies the radix base to use for the formatted number.

Examples

RES
Int.fromString("0") == Some(0) Int.fromString("NaN") == None Int.fromString("6", ~radix=2) == None

ignore

RES
let ignore: int => unit

ignore(int) ignores the provided int and returns unit.

This helper is useful when you want to discard a value (for example, the result of an operation with side effects) without having to store or process it further.

mod

RES
let mod: (int, int) => int

mod(n1, n2) calculates the modulo (remainder after division) of two integers.

Examples

RES
Int.mod(7, 4) == 3

range

RES
let range: (int, int, ~options: rangeOptions=?) => array<int>

range(start, end, ~options=?) returns an int array of the sequence of integers in the range [start, end). That is, including start but excluding end.

If step is not set and start < end, the sequence will be increasing in steps of 1.

If step is not set and start > end, the sequence will be decreasing in steps of -1.

If step is set, the sequence will increase or decrease by that amount for each step. If start < end and step is negative, or vice versa, an empty array is returned since the sequence would otherwise never reach or exceed the end value and hence be infinite. If step is 0 and start != end, a RangeError is thrown as the sequence would never reach or exceed the end value and hence be infinite.

If inclusive is set to true, the sequence will include end if step is set such that the sequence includes it.

Examples

RES
Int.range(3, 6) == [3, 4, 5] Int.range(-3, -1) == [-3, -2] Int.range(3, 1) == [3, 2] Int.range(3, 7, ~options={step: 2}) == [3, 5] Int.range(3, 7, ~options={step: 2, inclusive: true}) == [3, 5, 7] Int.range(3, 6, ~options={step: -2}) // RangeError

Exceptions

  • Raises RangeError if step == 0 && start != end.

rangeOptions

RES
type rangeOptions = {step?: int, inclusive?: bool}

The options for range.

rangeWithOptions

Deprecated

RES
let rangeWithOptions: (int, int, rangeOptions) => array<int>

rangeWithOptions(start, end, options) is like range, but with step and inclusive options configurable.

If step is set, the sequence will increase or decrease by that amount for each step. If start < end and step is negative, or vice versa, an empty array is returned since the sequence would otherwise never reach or exceed the end value and hence be infinite. If step is 0 and start != end, a RangeError is raised as the sequence would never reach or exceed the end value and hence be infinite.

If inclusive is set to true, the sequence will include end if step is set such that the sequence includes it.

Examples

RES
Int.rangeWithOptions(3, 7, {step: 2}) == [3, 5] Int.rangeWithOptions(3, 7, {step: 2, inclusive: true}) == [3, 5, 7] Int.rangeWithOptions(3, 6, {step: -2}) // RangeError

Exceptions

  • Raises RangeError if step == 0 && start != end.

shiftLeft

RES
let shiftLeft: (int, int) => int

shiftLeft(n, length) calculates the shifted value of an integer n by length bits to the left.

Examples

RES
Int.shiftLeft(4, 1) == 8

shiftRight

RES
let shiftRight: (int, int) => int

shiftRight(n, length) calculates the shifted value of an integer n by length bits to the right.

Also known as "arithmetic right shift" operation.

Examples

RES
Int.shiftRight(8, 1) == 4

shiftRightUnsigned

RES
let shiftRightUnsigned: (int, int) => int

shiftRightUnsigned(n, length) calculates the shifted value of an integer n by length bits to the right. Excess bits shifted off to the right are discarded, and zero bits are shifted in from the left.

Also known as "zero-filling right shift" operation.

Examples

RES
Int.shiftRightUnsigned(4, 1) == 2

t

RES
type t = int

Type representing an int.

toExponential

RES
let toExponential: (int, ~digits: int=?) => string

toExponential(n, ~digits=?) return a string representing the given value in exponential notation. digits specifies how many digits should appear after the decimal point. See Number.toExponential

Examples

RES
Int.toExponential(1000) // "1e+3" Int.toExponential(-1000) // "-1e+3" Int.toExponential(77, ~digits=2) // "7.70e+1" Int.toExponential(5678, ~digits=2) // "5.68e+3"

Exceptions

  • RangeError: If digits less than 0 or greater than 10.

toExponentialWithPrecision

Deprecated

RES
let toExponentialWithPrecision: (int, ~digits: int) => string

toExponential(n, ~digits) return a string representing the given value in exponential notation. digits specifies how many digits should appear after the decimal point. See Number.toExponential on MDN.

Examples

RES
Int.toExponentialWithPrecision(77, ~digits=2) // "7.70e+1" Int.toExponentialWithPrecision(5678, ~digits=2) // "5.68e+3"

Exceptions

  • RangeError: If digits less than 0 or greater than 10.

toFixed

RES
let toFixed: (int, ~digits: int=?) => string

toFixed(n, ~digits=?) return a string representing the given value using fixed-point notation. digits specifies how many digits should appear after the decimal point. See Number.toFixed on MDN.

Examples

RES
Int.toFixed(123456) // "123456.00" Int.toFixed(10) // "10.00" Int.toFixed(300, ~digits=4) // "300.0000" Int.toFixed(300, ~digits=1) // "300.0"

Exceptions

  • RangeError: If digits is less than 0 or larger than 100.

toFixedWithPrecision

Deprecated

RES
let toFixedWithPrecision: (int, ~digits: int) => string

toFixedWithPrecision(n, ~digits) return a string representing the given value using fixed-point notation. digits specifies how many digits should appear after the decimal point. See Number.toFixed on MDN.

Examples

RES
Int.toFixedWithPrecision(300, ~digits=4) // "300.0000" Int.toFixedWithPrecision(300, ~digits=1) // "300.0"

Exceptions

  • RangeError: If digits is less than 0 or larger than 100.

toFloat

RES
let toFloat: int => float

toFloat(n) return a float representing the given value.

Examples

RES
Int.toFloat(100) == 100.0 Int.toFloat(2) == 2.0

toLocaleString

RES
let toLocaleString: int => string

toLocaleString(n) return a string with language-sensitive representing the given value. See Number.toLocaleString on MDN.

Examples

RES
// If the application uses English as the default language Int.toLocaleString(1000) // "1,000" // If the application uses Portuguese Brazil as the default language Int.toLocaleString(1000) // "1.000"

toPrecision

RES
let toPrecision: (int, ~digits: int=?) => string

toPrecision(n, ~digits=?) return a string representing the giver value with precision. digits specifies the number of significant digits. See Number.toPrecision on MDN.

Examples

RES
Int.toPrecision(100) // "100" Int.toPrecision(1) // "1" Int.toPrecision(100, ~digits=2) // "1.0e+2" Int.toPrecision(1, ~digits=2) // "1.0"

Exceptions

  • RangeError: If digits is not between 1 and 100 (inclusive). Implementations are allowed to support larger and smaller values as well. ECMA-262 only requires a precision of up to 21 significant digits.

toPrecisionWithPrecision

Deprecated

RES
let toPrecisionWithPrecision: (int, ~digits: int) => string

toPrecisionWithPrecision(n, ~digits) return a string representing the giver value with precision. digits specifies the number of significant digits. See Number.toPrecision on MDN.

Examples

RES
Int.toPrecisionWithPrecision(100, ~digits=2) // "1.0e+2" Int.toPrecisionWithPrecision(1, ~digits=2) // "1.0"

Exceptions

  • RangeError: If digits is not between 1 and 100 (inclusive). Implementations are allowed to support larger and smaller values as well. ECMA-262 only requires a precision of up to 21 significant digits.

toString

RES
let toString: (int, ~radix: int=?) => string

toString(n, ~radix=?) return a string representing the given value. ~radix specifies the radix base to use for the formatted number. See Number.toString on MDN.

Examples

RES
Int.toString(1000) // "1000" Int.toString(-1000) // "-1000" Int.toString(6, ~radix=2) // "110" Int.toString(373592855, ~radix=16) // "16449317" Int.toString(123456, ~radix=36) // "2n9c"

Exceptions

RangeError: if radix is less than 2 or greater than 36.

toStringWithRadix

Deprecated

RES
let toStringWithRadix: (int, ~radix: int) => string

toStringWithRadix(n, ~radix) return a string representing the given value. ~radix specifies the radix base to use for the formatted number. See Number.toString on MDN.

Examples

RES
Int.toStringWithRadix(6, ~radix=2) // "110" Int.toStringWithRadix(373592855, ~radix=16) // "16449317" Int.toStringWithRadix(123456, ~radix=36) // "2n9c"

Exceptions

RangeError: if radix is less than 2 or greater than 36.