API / Core / Int

Int

Functions for interacting with JavaScript Number. See: Number.

equal

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

compare

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

toExponential

RESCRIPT
let toExponential: int => string

toExponential(n) return a string representing the given value in exponential notation. See Number.toExponential on MDN.

Examples

RESCRIPT
Int.toExponential(1000) // "1e+3" Int.toExponential(-1000) // "-1e+3"

toExponentialWithPrecision

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 => string

toFixed(n) return a string representing the given value using fixed-point notation. See Number.toFixed on MDN.

Examples

RESCRIPT
Int.toFixed(123456) // "123456.00" Int.toFixed(10) // "10.00"

toFixedWithPrecision

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.

toPrecision

RESCRIPT
let toPrecision: int => string

toPrecision(n) return a string representing the giver value with precision. This function omits the argument that controls precision, so it behaves like toString. See toPrecisionWithPrecision to control precision. See Number.toPrecision on MDN.

Examples

RESCRIPT
Int.toPrecision(100) // "100" Int.toPrecision(1) // "1"

toPrecisionWithPrecision

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 => string

toString(n) return a string representing the given value. See Number.toString on MDN.

Examples

RESCRIPT
Int.toString(1000) // "1000" Int.toString(-1000) // "-1000"

toStringWithRadix

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.

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"

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

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: (~radix: int=?, string) => option<int>

fromString(~radix?, str) 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(~radix=2, "6") == None

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) => array<int>

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

If start < end the sequence will be increasing in steps of 1.

If start > end the sequence will be decreasing in steps of -1.

This is equivalent to rangeWithOptions with inclusive set to false and step set to 1 if start < end and -1 otherwise.

Examples

RESCRIPT
Int.range(3, 6) == [3, 4, 5] Int.range(-3, -1) == [-3, -2] Int.range(3, 1) == [3, 2]

rangeOptions

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

The options for rangeWithOptions.

rangeWithOptions

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.

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