API / Core / Float

Float

Functions for interacting with float.

equal

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

compare

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

isNaN

RESCRIPT
let isNaN: float => bool

isNaN(v) tests if the given v is NaN. See NaN on MDN.

Examples

RESCRIPT
Float.isNaN(3.0) // false Float.isNaN(Float.Constants.nan) // true

isFinite

RESCRIPT
let isFinite: float => bool

isFinite(v) tests if the given v is finite. See isFinite on MDN.

Examples

RESCRIPT
Float.isFinite(1.0) // true Float.isFinite(Float.Constants.nan) // false Float.isFinite(Float.Constants.positiveInfinity) // false

parseFloat

RESCRIPT
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

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

RESCRIPT
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

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

RESCRIPT
let parseIntWithRadix: ('a, ~radix: int) => float

parseIntWithRadix(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

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

RESCRIPT
let toExponential: float => string

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

Examples

RESCRIPT
Float.toExponential(1000.0) // "1e+3" Float.toExponential(-1000.0) // "-1e+3"

toExponentialWithPrecision

RESCRIPT
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

RESCRIPT
Float.toExponentialWithPrecision(77.0, ~digits=2) // "7.70e+1" Float.toExponentialWithPrecision(5678.0, ~digits=2) // "5.68e+3"

Exceptions

  • RangeError: If digits less than 0 or greater than 10.

toFixed

RESCRIPT
let toFixed: float => string

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

Examples

RESCRIPT
Float.toFixed(123456.0) // "123456.00" Float.toFixed(10.0) // "10.00"

toFixedWithPrecision

RESCRIPT
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

RESCRIPT
Float.toFixedWithPrecision(300.0, ~digits=4) // "300.0000" Float.toFixedWithPrecision(300.0, ~digits=1) // "300.0"

Exceptions

  • RangeError: If digits is less than 0 or larger than 100.

toPrecision

RESCRIPT
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

RESCRIPT
Float.toPrecision(100.0) // "100" Float.toPrecision(1.0) // "1"

toPrecisionWithPrecision

RESCRIPT
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

RESCRIPT
Float.toPrecisionWithPrecision(100.0, ~digits=2) // "1.0e+2" Float.toPrecisionWithPrecision(1.0, ~digits=1) // "1"

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

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

Examples

RESCRIPT
Float.toString(1000.0) // "1000" Float.toString(-1000.0) // "-1000"

toStringWithRadix

RESCRIPT
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

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

RESCRIPT
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

RESCRIPT
let fromString: string => option<float>

fromString(str) return an option<int> representing the given value str.

Examples

RESCRIPT
Float.fromString("0") == Some(0.0) Float.fromString("NaN") == None Float.fromString("6") == Some(6.0)

toInt

RESCRIPT
let toInt: float => int

toInt(v) returns an int to given float v.

Examples

RESCRIPT
Float.toInt(2.0) == 2 Float.toInt(1.0) == 1 Float.toInt(1.1) == 1 Float.toInt(1.6) == 1

fromInt

RESCRIPT
let fromInt: int => float

fromInt(v) returns a float to given int v.

Examples

RESCRIPT
Float.fromInt(2) == 2.0 Float.fromInt(1) == 1.0

mod

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

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

Examples

RESCRIPT
Float.mod(7.0, 4.0) == 3.0

clamp

RESCRIPT
let clamp: (~min: float=?, ~max: float=?, float) => float

clamp(~min=?, ~max=?, value) returns value, optionally bounded by min and max.

if max < min returns min.

Examples

RESCRIPT
Float.clamp(4.2) == 4.2 Float.clamp(4.2, ~min=4.3) == 4.3 Float.clamp(4.2, ~max=4.1) == 4.1 Float.clamp(4.2, ~min=4.3, ~max=4.1) == 4.3