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 | bigint | boolean | number | string | { ... } | {} | Maximum value or options object. |
options.max? | bigint | boolean | number | string | min + 999999999999999n | Upper bound for generated bigint. |
options.min? | bigint | boolean | number | string | 0n | Lower bound for generated bigint. |
Returns: bigint
Throws: When min
is greater than max
.
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
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
.
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'
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 | |
options.precision? | number | Deprecated alias for DEPRECATED: Use |
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.
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
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
.
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'
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. |
Returns: number
Throws: When min
is greater than max
.
When there are no integers between min
and max
.
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
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
.
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'