Feels like Number.toPrecision()
is what you are looking for.
Below is slightly cleaned up version of your code snippet.
For the return value I'm using length.toPrecision(50)
:
function snake(radius, precision) {
const exp = 1 - precision;
const sub = Math.pow(2, exp);
const product = 2 - sub;
const length = Math.PI * radius * product;
return length.toPrecision(50);
}
console.log(snake(5, 55));
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…