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()
`.
Related modules
- For numeric strings of a given length, use
faker.string.numeric()
. - For credit card numbers, use
faker.finance.creditCardNumber()
.
bigInt
Returns a BigInt number. The bounds are inclusive.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | string | number | bigint | boolean | { ... } | {} | Maximum value or options object. |
options.max? | string | number | bigint | boolean | min + 999999999999999n | Upper bound for generated bigint. |
options.min? | string | number | bigint | boolean | 0n | Lower bound for generated bigint. |
Returns: bigint
Throws: When min
is greater than max
.
function bigInt(
options:
| bigint
| number
| string
| boolean
| {
min?: bigint | number | string | boolean;
max?: bigint | number | string | boolean;
} = {}
): bigint;
Examples
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
Source
binary
Returns a binary number. The bounds are inclusive.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | number | { ... } | {} | Maximum value or options object. |
options.max? | number | 1 | Upper bound for generated number. |
options.min? | number | 0 | Lower bound for generated number. |
Returns: string
Throws: When min
is greater than max
.
When there are no integers between min
and max
.
function binary(
options:
| number
| {
min?: number;
max?: number;
} = {}
): string;
Examples
faker.number.binary() // '1'
faker.number.binary(255) // '110101'
faker.number.binary({ min: 0, max: 65535 }) // '10110101'
Source
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
Name | Type | Default | Description |
---|---|---|---|
options | number | { ... } | {} | Upper bound or options object. |
options.fractionDigits? | number | The maximum number of digits to appear after the decimal point, for example | |
options.max? | number | 1.0 | Upper bound for generated number, exclusive, unless |
options.min? | number | 0.0 | Lower bound for generated number, inclusive. |
options.multipleOf? | number | The generated number will be a multiple of this parameter. Only one of |
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.
function float(
options:
| number
| {
min?: number;
max?: number;
fractionDigits?: number;
multipleOf?: number;
} = {}
): number;
Examples
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
Source
hex
Returns a lowercase hexadecimal number. The bounds are inclusive.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | number | { ... } | {} | Maximum value or options object. |
options.max? | number | 15 | Upper bound for generated number. |
options.min? | number | 0 | Lower bound for generated number. |
Returns: string
Throws: When min
is greater than max
.
When there are no integers between min
and max
.
function hex(
options:
| number
| {
min?: number;
max?: number;
} = {}
): string;
Examples
faker.number.hex() // 'b'
faker.number.hex(255) // '9d'
faker.number.hex({ min: 0, max: 65535 }) // 'af17'
Source
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
Name | Type | Default | Description |
---|---|---|---|
options | number | { ... } | {} | Maximum value or options object. |
options.max? | number | Number.MAX_SAFE_INTEGER | Upper bound for generated number. |
options.min? | number | 0 | Lower bound for generated number. |
options.multipleOf? | number | 1 | 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.
function int(
options:
| number
| {
min?: number;
max?: number;
multipleOf?: number;
} = {}
): number;
Examples
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
Source
octal
Returns an octal number. The bounds are inclusive.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | number | { ... } | {} | Maximum value or options object. |
options.max? | number | 7 | Upper bound for generated number. |
options.min? | number | 0 | Lower bound for generated number. |
Returns: string
Throws: When min
is greater than max
.
When there are no integers between min
and max
.
function octal(
options:
| number
| {
min?: number;
max?: number;
} = {}
): string;
Examples
faker.number.octal() // '5'
faker.number.octal(255) // '377'
faker.number.octal({ min: 0, max: 65535 }) // '4766'