Skip to content

Number

Module to generate numbers of any kind.

Overview

For simple integers, use int(). For decimal/floating-point numbers, use float().

For numbers not in base-10, you can use hex(), octal() and binary()`.

bigInt

Returns a BigInt number. The bounds are inclusive.

Available since v8.0.0

Parameters

NameTypeDefaultDescription
optionsstring | number | bigint | boolean | { ... }{}

Maximum value or options object.

options.max?string | number | bigint | booleanmin + 999999999999999n

Upper bound for generated bigint.

options.min?string | number | bigint | boolean0n

Lower bound for generated bigint.

Returns: bigint

Throws: When min is greater than max.

ts
function bigInt(
  options:
    | bigint
    | number
    | string
    | boolean
    | {
        min?: bigint | number | string | boolean;
        max?: bigint | number | string | boolean;
      } = {}
): bigint;

Examples

ts
faker.number.bigInt() // 55422n
faker.number.bigInt(100n) // 52n
faker.number.bigInt({ min: 1000000n }) // 431433n
faker.number.bigInt({ max: 100n }) // 42n
faker.number.bigInt({ min: 10n, max: 100n }) // 36n

binary

Returns a binary number. The bounds are inclusive.

Available since v8.0.0

Parameters

NameTypeDefaultDescription
optionsnumber | { ... }{}

Maximum value or options object.

options.max?number1

Upper bound for generated number.

options.min?number0

Lower bound for generated number.

Returns: string

Throws: When min is greater than max. When there are no integers between min and max.

ts
function binary(
  options:
    | number
    | {
        min?: number;
        max?: number;
      } = {}
): string;

Examples

ts
faker.number.binary() // '1'
faker.number.binary(255) // '110101'
faker.number.binary({ min: 0, max: 65535 }) // '10110101'

float

Returns a single random floating-point number, by default between 0.0 and 1.0. To change the range, pass a min and max value. To limit the number of decimal places, pass a multipleOf or fractionDigits parameter.

Available since v8.0.0

Parameters

NameTypeDefaultDescription
optionsnumber | { ... }{}

Upper bound or options object.

options.fractionDigits?number

The maximum number of digits to appear after the decimal point, for example 2 will round to 2 decimal points. Only one of multipleOf or fractionDigits should be passed.

options.max?number1.0

Upper bound for generated number, exclusive, unless multipleOf or fractionDigits are passed.

options.min?number0.0

Lower bound for generated number, inclusive.

options.multipleOf?number

The generated number will be a multiple of this parameter. Only one of multipleOf or fractionDigits should be passed.

Returns: number

Throws: When min is greater than max. When multipleOf is negative. When fractionDigits is negative. When fractionDigits and multipleOf is passed in the same options object.

ts
function float(
  options:
    | number
    | {
        min?: number;
        max?: number;
        fractionDigits?: number;
        multipleOf?: number;
      } = {}
): number;

Examples

ts
faker.number.float() // 0.5688541042618454
faker.number.float(3) // 2.367973240558058
faker.number.float({ max: 100 }) // 17.3687307164073
faker.number.float({ min: 20, max: 30 }) // 23.94764115102589
faker.number.float({ multipleOf: 0.25, min: 0, max:10 }) // 7.75
faker.number.float({ fractionDigits: 1 }) // 0.9
faker.number.float({ min: 10, max: 100, multipleOf: 0.02 }) // 35.42
faker.number.float({ min: 10, max: 100, fractionDigits: 3 }) // 65.716
faker.number.float({ min: 10, max: 100, multipleOf: 0.001 }) // 65.716 - same as above

hex

Returns a lowercase hexadecimal number. The bounds are inclusive.

Available since v8.0.0

Parameters

NameTypeDefaultDescription
optionsnumber | { ... }{}

Maximum value or options object.

options.max?number15

Upper bound for generated number.

options.min?number0

Lower bound for generated number.

Returns: string

Throws: When min is greater than max. When there are no integers between min and max.

ts
function hex(
  options:
    | number
    | {
        min?: number;
        max?: number;
      } = {}
): string;

Examples

ts
faker.number.hex() // 'b'
faker.number.hex(255) // '9d'
faker.number.hex({ min: 0, max: 65535 }) // 'af17'

int

Returns a single random integer between zero and the given max value or the given range. The bounds are inclusive.

Available since v8.0.0

Parameters

NameTypeDefaultDescription
optionsnumber | { ... }{}

Maximum value or options object.

options.max?numberNumber.MAX_SAFE_INTEGER

Upper bound for generated number.

options.min?number0

Lower bound for generated number.

options.multipleOf?number1

Generated number will be a multiple of the given integer.

Returns: number

Throws: When min is greater than max. When there are no suitable integers between min and max. When multipleOf is not a positive integer.

ts
function int(
  options:
    | number
    | {
        min?: number;
        max?: number;
        multipleOf?: number;
      } = {}
): number;

Examples

ts
faker.number.int() // 2900970162509863
faker.number.int(100) // 52
faker.number.int({ min: 1000000 }) // 2900970162509863
faker.number.int({ max: 100 }) // 42
faker.number.int({ min: 10, max: 100 }) // 57
faker.number.int({ min: 10, max: 100, multipleOf: 10 }) // 50

octal

Returns an octal number. The bounds are inclusive.

Available since v8.0.0

Parameters

NameTypeDefaultDescription
optionsnumber | { ... }{}

Maximum value or options object.

options.max?number7

Upper bound for generated number.

options.min?number0

Lower bound for generated number.

Returns: string

Throws: When min is greater than max. When there are no integers between min and max.

ts
function octal(
  options:
    | number
    | {
        min?: number;
        max?: number;
      } = {}
): string;

Examples

ts
faker.number.octal() // '5'
faker.number.octal(255) // '377'
faker.number.octal({ min: 0, max: 65535 }) // '4766'

Released under the MIT License.