* 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. * * @param options Upper bound or options object. * @param option
(
options:
| number
| {
/**
* Lower bound for generated number, inclusive.
*
* @default 0.0
*/
min?: number;
/**
* Upper bound for generated number, exclusive, unless `multipleOf` or `fractionDigits` are passed.
*
* @default 1.0
*/
max?: 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.
*/
fractionDigits?: number;
/**
* The generated number will be a multiple of this parameter. Only one of `multipleOf` or `fractionDigits` should be passed.
*/
multipleOf?: number;
/**
* A function to determine the distribution of generated values.
*
* @default uniformDistributor()
*/
distributor?: Distributor;
} = {}
)
| 144 | * @since 8.0.0 |
| 145 | */ |
| 146 | float( |
| 147 | options: |
| 148 | | number |
| 149 | | { |
| 150 | /** |
| 151 | * Lower bound for generated number, inclusive. |
| 152 | * |
| 153 | * @default 0.0 |
| 154 | */ |
| 155 | min?: number; |
| 156 | /** |
| 157 | * Upper bound for generated number, exclusive, unless `multipleOf` or `fractionDigits` are passed. |
| 158 | * |
| 159 | * @default 1.0 |
| 160 | */ |
| 161 | max?: number; |
| 162 | /** |
| 163 | * 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. |
| 164 | */ |
| 165 | fractionDigits?: number; |
| 166 | /** |
| 167 | * The generated number will be a multiple of this parameter. Only one of `multipleOf` or `fractionDigits` should be passed. |
| 168 | */ |
| 169 | multipleOf?: number; |
| 170 | /** |
| 171 | * A function to determine the distribution of generated values. |
| 172 | * |
| 173 | * @default uniformDistributor() |
| 174 | */ |
| 175 | distributor?: Distributor; |
| 176 | } = {} |
| 177 | ): number { |
| 178 | if (typeof options === 'number') { |
| 179 | options = { |
| 180 | max: options, |
| 181 | }; |
| 182 | } |
| 183 | |
| 184 | const { |
| 185 | min = 0, |
| 186 | max = 1, |
| 187 | fractionDigits, |
| 188 | multipleOf: originalMultipleOf, |
| 189 | multipleOf = fractionDigits == null ? undefined : 10 ** -fractionDigits, |
| 190 | distributor = uniformDistributor(), |
| 191 | } = options; |
| 192 | |
| 193 | if (max < min) { |
| 194 | throw new FakerError(`Max ${max} should be greater than min ${min}.`); |
| 195 | } |
| 196 | |
| 197 | if (fractionDigits != null) { |
| 198 | if (originalMultipleOf != null) { |
| 199 | throw new FakerError( |
| 200 | 'multipleOf and fractionDigits cannot be set at the same time.' |
| 201 | ); |
| 202 | } |
| 203 |
no test coverage detected