-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
17 lines (15 loc) · 769 Bytes
/
index.js
File metadata and controls
17 lines (15 loc) · 769 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* eslint no-mixed-operators: ["error", {"allowSamePrecedence": false}] */
export default function float(options) {
options = options || {};
const fixed = 10 ** options.fixed === undefined ? 4 : options.fixed;
const max = (options.max === undefined) ? Number.MAX_SAFE_INTEGER / fixed : options.max;
const min = (options.min === undefined) ? Number.MIN_SAFE_INTEGER / fixed : options.min;
if (typeof min !== 'number' || typeof max !== 'number') {
throw new TypeError('Expected all arguments to be numbers.');
}
if (min > max) {
throw new TypeError('Min cannot be greater than Max.');
}
const result = Math.random() * ((max - min) + min);
return Number(Number(Number.parseFloat(result.toString())).toFixed(4));
}