Module Js_int

Provides functions for inspecting and manipulating ints

If we use number, we need coerce to int32 by adding `|0`, otherwise `+0` can be wrong. Most JS API is float oriented, it may overflow int32 or comes with NAN

let toExponential: int => string;

Formats an int using exponential (scientific) notation

Returns a string representing the given value in exponential notation

  • raises RangeError

    if digits is not in the range [0, 20] (inclusive)

    (* prints "7.7e+1" *)
    let _ = Js.log (Js.Int.toExponential 77)
let toExponentialWithPrecision: int => digits:int => string;

Formats an int 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).

Returns a string representing the given value in exponential notation

The output will be rounded or padded with zeroes if necessary.

  • raises RangeError

    if digits is not in the range [0, 20] (inclusive)

    (* prints "7.70e+1" *)
    let _ = Js.log  (Js.Int.toExponentialWithPrecision 77 ~digits:2)
    
    (* prints "5.68e+3" *)
    let _ = Js.log  (Js.Int.toExponentialWithPrecision 5678 ~digits:2)
let toPrecision: int => string;

Formats a int using some fairly arbitrary rules

Returns a string representing the given value in fixed-point (usually)

toPrecision differs from toFixed in that the former will format the number with full precision, while the latter will not output any digits after the decimal point.

  • raises RangeError

    if digits is not in the range accepted by this function (what do you mean "vague"?)

    (* prints "123456789" *)
    let _ = Js.log (Js.Int.toPrecision 123456789)
let toPrecisionWithPrecision: int => digits:int => string;

Formats an int 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).

Returns 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.

  • raises RangeError

    if digits is not in the range accepted by this function (what do you mean "vague"?)

    (* prints "1.2e+8" *)
    let _ = Js.log (Js.Int.toPrecisionWithPrecision 123456789 ~digits:2)
    
    (* prints "0.0" *)
    let _ = Js.log (Js.Int.toPrecisionWithPrecision 0 ~digits:2)
let toString: int => string;

Formats a int as a string

Returns a string representing the given value in fixed-point (usually)

(* prints "123456789" *)
let _ = Js.log (Js.Int.toString 123456789)
let toStringWithRadix: int => radix:int => string;

Formats an int as a string

radix specifies the radix base to use for the formatted number. The value must be in the range [2, 36] (inclusive).

Returns a string representing the given value in fixed-point (usually)

  • raises RangeError

    if radix is not in the range [2, 36] (inclusive)

    (* prints "110" *)
    let _ = Js.log (Js.Int.toStringWithRadix 6 ~radix:2)
    
    (* prints "deadbeef" *)
    let _ = Js.log (Js.Int.toStringWithRadix 3735928559 ~radix:16)
    
    (* prints "2n9c" *)
    let _ = Js.log (Js.Int.toStringWithRadix 123456 ~radix:36)
let toFloat: int => float;
let equal: int => int => bool;
let max: int;
let min: int;