Int

Functions for interacting with JavaScript Number. See: Number.

bitwiseAnd

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

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

Examples

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

bitwiseNot

RESCRIPT
let bitwiseNot: int => int

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

Examples

RESCRIPT
Int.bitwiseNot(2) == -3

bitwiseOr

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

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

Examples

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

bitwiseXor

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

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

Examples

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

clamp

RESCRIPT
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

RESCRIPT
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

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

equal

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

fromFloat

RESCRIPT
let fromFloat: float => int

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

Examples

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

fromString

RESCRIPT
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

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

ignore

RESCRIPT
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

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

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

Examples

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

range

RESCRIPT
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

RESCRIPT
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

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

The options for range.

rangeWithOptions

Deprecated

RESCRIPT
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

RESCRIPT
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

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

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

Examples

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

shiftRight

RESCRIPT
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

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

shiftRightUnsigned

RESCRIPT
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

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

t

RESCRIPT
type t = int

Type representing an int.

toExponential

RESCRIPT
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

RESCRIPT
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

RESCRIPT
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

RESCRIPT
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

RESCRIPT
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

RESCRIPT
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

RESCRIPT
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

RESCRIPT
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

RESCRIPT
let toFloat: int => float

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

Examples

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

toLocaleString

RESCRIPT
let toLocaleString: int => string

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

Examples

RESCRIPT
// 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

RESCRIPT
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

RESCRIPT
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

RESCRIPT
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

RESCRIPT
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

RESCRIPT
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

RESCRIPT
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

RESCRIPT
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

RESCRIPT
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.