Float
Provide utilities for JS float.
_NaN
let _NaN: float
The special value "Not a Number". See NaN
on MDN.
isNaN
let isNaN: float => bool
Tests if the given value is _NaN
Note that both _NaN = _NaN
and _NaN == _NaN
will return false
. isNaN
is
therefore necessary to test for _NaN
. Return true
if the given value is
_NaN
, false
otherwise. See isNaN
on MDN.
isFinite
let isFinite: float => bool
Tests if the given value is finite. Return true
if the given value is a finite
number, false
otherwise. See isFinite
on MDN.
Examples
RESCRIPT/* returns [false] */
Js.Float.isFinite(infinity)
/* returns [false] */
Js.Float.isFinite(neg_infinity)
/* returns [false] */
Js.Float.isFinite(Js.Float._NaN)
/* returns [true] */
Js.Float.isFinite(1234.)
toExponential
let toExponential: float => string
Formats a float
using exponential (scientific) notation. Return a
string
representing the given value in exponential notation. Raise
RangeError if digits is not in the range [0, 20] (inclusive). See toExponential
on MDN.
Examples
RESCRIPT/* prints "7.71234e+1" */
Js.Float.toExponential(77.1234)->Js.log
/* prints "7.7e+1" */
Js.Float.toExponential(77.)->Js.log
toExponentialWithPrecision
let toExponentialWithPrecision: (float, ~digits: int) => string
Formats a float
using exponential (scientific) notation. digits
specifies
how many digits should appear after the decimal point. The value must be in
the range [0, 20] (inclusive). Return a string
representing the given value
in exponential notation. The output will be rounded or padded with zeroes if
necessary. Raise RangeError if digits
is not in the range [0, 20] (inclusive).
See toExponential
on MDN.
Examples
RESCRIPT/* prints "7.71e+1" */
Js.Float.toExponentialWithPrecision(77.1234, ~digits=2)->Js.log
toFixed
let toFixed: float => string
Formats a float
using fixed point notation. Return a string
representing the
given value in fixed-point notation (usually). Raise RangeError if digits is not
in the range [0, 20] (inclusive). See toFixed
on MDN.
Examples
RESCRIPT/* prints "12346" (note the rounding) */
Js.Float.toFixed(12345.6789)->Js.log
/* print "1.2e+21" */
Js.Float.toFixed(1.2e21)->Js.log
toFixedWithPrecision
let toFixedWithPrecision: (float, ~digits: int) => string
Formats a float
using fixed point notation. digits
specifies how many digits
should appear after the decimal point. The value must be in the range [0, 20]
(inclusive). Defaults to 0
. Return a string
representing the given value in
fixed-point notation (usually). See toFixed
on MDN.
The output will be rounded or padded with zeroes if necessary.
Raise RangeError if digits is not in the range [0, 20] (inclusive)
Examples
RESCRIPT/* prints "12345.7" (note the rounding) */
Js.Float.toFixedWithPrecision(12345.6789, ~digits=1)->Js.log
/* prints "0.00" (note the added zeroes) */
Js.Float.toFixedWithPrecision(0., ~digits=2)->Js.log
toPrecision
let toPrecision: float => string
Formats a float
using some fairly arbitrary rules. Return a string
representing the given value in fixed-point (usually). toPrecision
differs
from Js.Float.toFixed
in that the former will format the number with full
precision, while the latter will not output any digits after the decimal point.
See toPrecision
on MDN.
Raise RangeError if digits is not in the range accepted by this function (what do you mean "vague"?)
Examples
RESCRIPT/* prints "12345.6789" */
Js.Float.toPrecision(12345.6789)->Js.log
/* print "1.2e+21" */
Js.Float.toPrecision(1.2e21)->Js.log
toPrecisionWithPrecision
let toPrecisionWithPrecision: (float, ~digits: int) => string
Formats a float
using some fairly arbitrary rules. digits
specifies how many
digits should appear in total. The value must between 0 and some arbitrary number
that's hopefully at least larger than 20 (for Node it's 21. Why? Who knows).
See toPrecision
on MDN.
Return a string
representing the given value in fixed-point or scientific
notation. The output will be rounded or padded with zeroes if necessary.
toPrecisionWithPrecision
differs from toFixedWithPrecision
in that the former
will count all digits against the precision, while the latter will count only
the digits after the decimal point. toPrecisionWithPrecision
will also use
scientific notation if the specified precision is less than the number for digits
before the decimal point.
Raise RangeError if digits is not in the range accepted by this function (what do you mean "vague"?)
Examples
RESCRIPT/* prints "1e+4" */
Js.Float.toPrecisionWithPrecision(12345.6789, ~digits=1)->Js.log
/* prints "0.0" */
Js.Float.toPrecisionWithPrecision(0., ~digits=2)->Js.log
toString
let toString: float => string
Formats a float
as a string. Return a string
representing the given value in
fixed-point (usually). See toString
on MDN.
Examples
RESCRIPT/* prints "12345.6789" */
Js.Float.toString(12345.6789)->Js.log
toStringWithRadix
let toStringWithRadix: (float, ~radix: int) => string
Formats a float
as a string. radix
specifies the radix base to use for the
formatted number. The value must be in the range [2, 36] (inclusive). Return a
string
representing the given value in fixed-point (usually). See toString
on MDN.
Raise RangeError if radix is not in the range [2, 36] (inclusive)
Examples
RESCRIPT/* prints "110" */
Js.Float.toStringWithRadix(6., ~radix=2)->Js.log
/* prints "11.001000111101011100001010001111010111000010100011111" */
Js.Float.toStringWithRadix(3.14, ~radix=2)->Js.log
/* prints "deadbeef" */
Js.Float.toStringWithRadix(3735928559., ~radix=16)->Js.log
/* prints "3f.gez4w97ry0a18ymf6qadcxr" */
Js.Float.toStringWithRadix(123.456, ~radix=36)->Js.log
fromString
let fromString: string => float
Parses the given string
into a float
using JavaScript semantics. Return the
number as a float
if successfully parsed, _NaN
otherwise.
Examples
RESCRIPT/* returns 123 */
Js.Float.fromString("123")
/* returns 12.3 */
Js.Float.fromString("12.3")
/* returns 0 */
Js.Float.fromString("")
/* returns 17 */
Js.Float.fromString("0x11")
/* returns 3 */
Js.Float.fromString("0b11")
/* returns 9 */
Js.Float.fromString("0o11")
/* returns [_NaN] */
Js.Float.fromString("hello")
/* returns [_NaN] */
Js.Float.fromString("100a")