Это мое решение, но я думаю, что это можно сделать проще.
const set = [0.0000007123123123, 0.0000000012321, 0.0125, 1.1005, 0.1511, 1.51231e-10, 10.1505, 1.511e3]
let roundDecimal = (value, precision = 1) => {
let firstSignificantDigitPlace = e => parseInt(e.toExponential().split('-')[1]) || Math.min(precision, 1)
if (Array.isArray(value)) {
return set.map(e => {
if (typeof e !== 'number') throw "roundDecimal: Array should contains only digits."
return e.toFixed(firstSignificantDigitPlace(e) + precision )
})
} else if (typeof value === 'number') {
return value.toFixed(firstSignificantDigitPlace(value) + precision )
}
throw "roundDecimal: Function accepts only array or digit as value."
}
console.log(roundDecimal(.0051101))
console.log(roundDecimal(set))
console.log(roundDecimal(set, 3))
console.log(roundDecimal(5.153123e-15))
console.log(roundDecimal(Number.NaN))