Int
Functions for interacting with JavaScript Number.
See: Number
.
equal
let equal: (int, int) => bool
compare
let compare: (int, int) => Core__Ordering.t
toExponential
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
RESCRIPTInt.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
: Ifdigits
less than 0 or greater than 10.
toExponentialWithPrecision
Deprecated
Use toExponential
instead
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
RESCRIPTInt.toExponentialWithPrecision(77, ~digits=2) // "7.70e+1"
Int.toExponentialWithPrecision(5678, ~digits=2) // "5.68e+3"
Exceptions
RangeError
: Ifdigits
less than 0 or greater than 10.
toFixed
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
RESCRIPTInt.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
: Ifdigits
is less than 0 or larger than 100.
toFixedWithPrecision
Deprecated
Use toFixed
instead
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
RESCRIPTInt.toFixedWithPrecision(300, ~digits=4) // "300.0000"
Int.toFixedWithPrecision(300, ~digits=1) // "300.0"
Exceptions
RangeError
: Ifdigits
is less than 0 or larger than 100.
toPrecision
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
RESCRIPTInt.toPrecision(100) // "100"
Int.toPrecision(1) // "1"
Int.toPrecision(100, ~digits=2) // "1.0e+2"
Int.toPrecision(1, ~digits=2) // "1.0"
Exceptions
RangeError
: Ifdigits
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
Use toPrecision
instead
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
RESCRIPTInt.toPrecisionWithPrecision(100, ~digits=2) // "1.0e+2"
Int.toPrecisionWithPrecision(1, ~digits=2) // "1.0"
Exceptions
RangeError
: Ifdigits
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
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
RESCRIPTInt.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
Use toString
instead
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
RESCRIPTInt.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
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
let toFloat: int => float
toFloat(n)
return a float
representing the given value.
Examples
RESCRIPTInt.toFloat(100) == 100.0
Int.toFloat(2) == 2.0
fromFloat
let fromFloat: float => int
fromFloat(n)
return an int
representing the given value. The conversion is
done by truncating the decimal part.
Examples
RESCRIPTInt.fromFloat(2.0) == 2
Int.fromFloat(1.999) == 1
Int.fromFloat(1.5) == 1
Int.fromFloat(0.9999) == 0
fromString
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
RESCRIPTInt.fromString("0") == Some(0)
Int.fromString("NaN") == None
Int.fromString("6", ~radix=2) == None
mod
let mod: (int, int) => int
mod(n1, n2)
calculates the modulo (remainder after division) of two integers.
Examples
RESCRIPTInt.mod(7, 4) == 3
rangeOptions
type rangeOptions = {step?: int, inclusive?: bool}
The options for range
.
range
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
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
RESCRIPTInt.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
ifstep == 0 && start != end
.
rangeWithOptions
Deprecated
Use range
instead
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
RESCRIPTInt.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
ifstep == 0 && start != end
.
clamp
let clamp: (~min: int=?, ~max: int=?, int) => int