Float
Functions for interacting with float.
equal
let equal: (float, float) => boolcompare
let compare: (float, float) => Core__Ordering.tisNaN
let isNaN: float => boolisNaN(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 => boolisFinite(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 => floatparseFloat(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, ~radix: int=?) => floatparseInt(v, ~radix=?) parse the given v and returns a float. Leading
whitespace in this argument vis 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.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
Float.parseInt("10.0", ~radix=2) // 2.0
Float.parseInt("15 * 3", ~radix=10) // 15.0
Float.parseInt("12", ~radix=13) // 15.0
Float.parseInt("17", ~radix=40)->Float.isNaN // true
parseIntWithRadix
Deprecated
Use parseInt instead
let parseIntWithRadix: ('a, ~radix: int) => floatparseIntWithRadix(v, ~radix) parse the given v and returns a float. Leading
whitespace in this argument vis 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, ~digits: int=?) => stringtoExponential(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.toExponential(1000.0) // "1e+3"
Float.toExponential(-1000.0) // "-1e+3"
Float.toExponential(77.0, ~digits=2) // "7.70e+1"
Float.toExponential(5678.0, ~digits=2) // "5.68e+3"
Exceptions
RangeError: Ifdigitsless than 0 or greater than 10.
toExponentialWithPrecision
Deprecated
Use toExponential instead
let toExponentialWithPrecision: (float, ~digits: int) => stringtoExponential(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: Ifdigitsless than 0 or greater than 10.
toFixed
let toFixed: (float, ~digits: int=?) => stringtoFixed(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.toFixed(123456.0) // "123456.00"
Float.toFixed(10.0) // "10.00"
Float.toFixed(300.0, ~digits=4) // "300.0000"
Float.toFixed(300.0, ~digits=1) // "300.0"
Exceptions
RangeError: Ifdigitsis less than 0 or larger than 100.
toFixedWithPrecision
Deprecated
Use toFixed instead
let toFixedWithPrecision: (float, ~digits: int) => stringtoFixedWithPrecision(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: Ifdigitsis less than 0 or larger than 100.
toPrecision
let toPrecision: (float, ~digits: int=?) => stringtoPrecision(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.toPrecision(100.0) // "100"
Float.toPrecision(1.0) // "1"
Float.toPrecision(100.0, ~digits=2) // "1.0e+2"
Float.toPrecision(1.0, ~digits=1) // "1"
Exceptions
RangeError: Ifdigitsis 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: (float, ~digits: int) => stringtoPrecisionWithPrecision(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: Ifdigitsis 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, ~radix: int=?) => stringtoString(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
Deprecated
Use toString with ~radix instead
let toStringWithRadix: (float, ~radix: int) => stringtoStringWithRadix(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 => stringtoLocaleString(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 => inttoInt(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 => floatfromInt(v) returns a float to given int v.
Examples
RESCRIPTFloat.fromInt(2) == 2.0
Float.fromInt(1) == 1.0
mod
let mod: (float, float) => floatmod(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