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
optionsbigint | boolean | number | string | { ... }{}

Maximum value or options object.

options.max?bigint | boolean | number | stringmin + 999999999999999n

Upper bound for generated bigint.

options.min?bigint | boolean | number | string0n

Lower bound for generated bigint.

Returns: bigint

Throws: When min is greater than max.

ts
faker.number.bigInt(options: bigint | boolean | number | string | {
  max: bigint | boolean | number | string,
  min: bigint | boolean | number | string
} = {}): bigint
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
faker.number.binary(options: number | {
  max: number,
  min: number
} = {}): string
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, precision or fractionDigits should be passed.

options.max?number1.0

Upper bound for generated number, exclusive, unless multipleOf, precision 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, precision or fractionDigits should be passed.

options.precision?number

Deprecated alias for multipleOf. Only one of multipleOf, precision or fractionDigits should be passed.

DEPRECATED: Use multipleOf instead.

Returns: number

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

ts
faker.number.float(options: number | {
  fractionDigits: number,
  max: number,
  min: number,
  multipleOf: number,
  precision: number
} = {}): number
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
faker.number.hex(options: number | {
  max: number,
  min: number
} = {}): string
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.

Returns: number

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

ts
faker.number.int(options: number | {
  max: number,
  min: number
} = {}): number
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

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
faker.number.octal(options: number | {
  max: number,
  min: number
} = {}): string
faker.number.octal() // '5'
faker.number.octal(255) // '377'
faker.number.octal({ min: 0, max: 65535 }) // '4766'

Released under the MIT License.