Float
Functions for interacting with float.
equal
let equal: (float, float) => bool
compare
let compare: (float, float) => Core__Ordering.t
isNaN
let isNaN: float => bool
isNaN(v)
tests if the given v
is NaN
.
See NaN
on MDN.
Examples
RESCRIPTFloat.isNaN(3.0) // false
Float.isNaN(Float.Constants.nan) // true
isFinite
let isFinite: float => bool
isFinite(v)
tests if the given v
is finite.
See isFinite
on MDN.
Examples
RESCRIPTFloat.isFinite(1.0) // true
Float.isFinite(Float.Constants.nan) // false
Float.isFinite(Float.Constants.positiveInfinity) // false
parseFloat
let parseFloat: string => float
parseFloat(v)
parse the given v
and returns a float. Leading whitespace in
v
is ignored. Returns NaN
if v
can't be parsed. Use [fromString
] to
ensure it returns a valid float and not NaN
.
See parseFloat
on MDN.
Examples
RESCRIPTFloat.parseFloat("1.0") // 1.0
Float.parseFloat(" 3.14 ") // 3.14
Float.parseFloat("3.0") // 3.0
Float.parseFloat("3.14some non-digit characters") // 3.14
Float.parseFloat("error")->Float.isNaN // true
parseInt
let parseInt: 'a => float
parseInt(v)
parse the given v
and returns a float. Leading whitespace in
v
is ignored. Returns NaN
if v
can't be parsed.
See parseInt
on MDN.
Examples
RESCRIPTFloat.parseInt("1.0") // 1.0
Float.parseInt(" 3.14 ") // 3.0
Float.parseInt(3) // 3.0
Float.parseInt("3.14some non-digit characters") // 3.0
Float.parseInt("error")->Float.isNaN // true
parseIntWithRadix
let parseIntWithRadix: ('a, ~radix: int) => float
parseIntWithRadix(v, ~radix)
parse the given v
and returns a float. Leading
whitespace in this argument v
is ignored. radix
specifies the radix base to
use for the formatted number. The value must be in the range [2, 36] (inclusive).
Returns NaN
if v
can't be parsed and radix
is smaller than 2 or bigger
than 36.
See parseInt
on MDN.
Examples
RESCRIPTFloat.parseIntWithRadix("10.0", ~radix=2) // 2.0
Float.parseIntWithRadix("15 * 3", ~radix=10) // 15.0
Float.parseIntWithRadix("12", ~radix=13) // 15.0
Float.parseIntWithRadix("17", ~radix=40)->Float.isNaN // true
toExponential
let toExponential: float => string
toExponential(v)
return a string
representing the given value in exponential
notation.
See Number.toExponential
on MDN.
Examples
RESCRIPTFloat.toExponential(1000.0) // "1e+3"
Float.toExponential(-1000.0) // "-1e+3"
toExponentialWithPrecision
let toExponentialWithPrecision: (float, ~digits: int) => string
toExponential(v, ~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
RESCRIPTFloat.toExponentialWithPrecision(77.0, ~digits=2) // "7.70e+1"
Float.toExponentialWithPrecision(5678.0, ~digits=2) // "5.68e+3"
Exceptions
RangeError
: Ifdigits
less than 0 or greater than 10.
toFixed
let toFixed: float => string
toFixed(v)
return a string
representing the given value using fixed-point
notation.
See Number.toFixed
on MDN.
Examples
RESCRIPTFloat.toFixed(123456.0) // "123456.00"
Float.toFixed(10.0) // "10.00"
toFixedWithPrecision
let toFixedWithPrecision: (float, ~digits: int) => string
toFixedWithPrecision(v, ~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
RESCRIPTFloat.toFixedWithPrecision(300.0, ~digits=4) // "300.0000"
Float.toFixedWithPrecision(300.0, ~digits=1) // "300.0"
Exceptions
RangeError
: Ifdigits
is less than 0 or larger than 100.
toPrecision
let toPrecision: float => string
toPrecision(v)
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
RESCRIPTFloat.toPrecision(100.0) // "100"
Float.toPrecision(1.0) // "1"
toPrecisionWithPrecision
let toPrecisionWithPrecision: (float, ~digits: int) => string
toPrecisionWithPrecision(v, ~digits)
return a string
representing the giver value with
precision. digits
specifies the number of significant digits.
See Number.toPrecision
on MDN.
Examples
RESCRIPTFloat.toPrecisionWithPrecision(100.0, ~digits=2) // "1.0e+2"
Float.toPrecisionWithPrecision(1.0, ~digits=1) // "1"
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: float => string
toString(v)
return a string
representing the given value.
See Number.toString
on MDN.
Examples
RESCRIPTFloat.toString(1000.0) // "1000"
Float.toString(-1000.0) // "-1000"
toStringWithRadix
let toStringWithRadix: (float, ~radix: int) => string
toStringWithRadix(v, ~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
RESCRIPTFloat.toStringWithRadix(6.0, ~radix=2) // "110"
Float.toStringWithRadix(3735928559.0, ~radix=16) // "deadbeef"
Float.toStringWithRadix(123456.0, ~radix=36) // "2n9c"
Exceptions
RangeError
: if radix
is less than 2 or greater than 36.
toLocaleString
let toLocaleString: float => string
toLocaleString(v)
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
Float.toLocaleString(1000.0) // "1,000"
// If the application uses Portuguese Brazil as the default language
Float.toLocaleString(1000.0) // "1.000"
fromString
let fromString: string => option<float>
fromString(str)
return an option<int>
representing the given value str
.
Examples
RESCRIPTFloat.fromString("0") == Some(0.0)
Float.fromString("NaN") == None
Float.fromString("6") == Some(6.0)
toInt
let toInt: float => int
toInt(v)
returns an int to given float v
.
Examples
RESCRIPTFloat.toInt(2.0) == 2
Float.toInt(1.0) == 1
Float.toInt(1.1) == 1
Float.toInt(1.6) == 1
fromInt
let fromInt: int => float
fromInt(v)
returns a float to given int v
.
Examples
RESCRIPTFloat.fromInt(2) == 2.0
Float.fromInt(1) == 1.0
mod
let mod: (float, float) => float
mod(n1, n2)
calculates the modulo (remainder after division) of two floats.
Examples
RESCRIPTFloat.mod(7.0, 4.0) == 3.0
clamp
let clamp: (~min: float=?, ~max: float=?, float) => float