-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutilities-general.js
33 lines (30 loc) · 1.33 KB
/
utilities-general.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// --------------------------------------------------------------------------
// -- utilities-general.js
// -- initial author: Renick Bell ([email protected])
// -- initial creation date: Wed Jun 28 10:08:48 AM CST 2023
// -- contributors: Yiler Huang ([email protected]); Steve Wang ([email protected])
// -- license: GPL 3.0
// --------------------------------------------------------------------------
/**
* Round a number to a specific number of decimal places.
* @param {number} number - Number to round.
* @param {number} decimalPlaces - Amount of decimal places to round numbe to.
* @example console.log(roundTo(10.2332, 2)) //10.23
*/
function roundTo (number,decimalPlaces){
let roundedNumber=number.toFixed(decimalPlaces);
return JSON.parse(roundedNumber)
}
/**
* Returns a random number between a range.
* @param {number} min - Minimum amount the random number can be.
* @param {number} max - Maximum amount the random number can be.
* @param {number} decimalPlaces - The amount of decimal places the random number can have.
* @example
* console.log(randomRange(0, 4, 4)) //1.7395
* console.log(randomRange(0, 4, 40)) //1.71246107249822
*/
function randomRange (min, max,decimalPlaces) {
if (decimalPlaces==undefined){decimalPlaces=0}
return roundTo(min + (max - min) * (Math.random()),decimalPlaces);
}