trimming floating point

Benchmark created on


Setup

let s = Math.random() > 0.5 ? 1 : -1;
let pow = Math.round(Math.random() * 1000) % 35;
let pows = Math.random() > 0.5 ? 1 : -1;
let fs = Math.random();
let f64 = s * fs * Math.pow(10, pow * pows);
let f32 = Math.fround(f64);

Test runner

Ready to run.

Testing in
TestOps/sec
String parse
String(Number(f32.toPrecision(9)));
ready
Regex parse
f32.toPrecision(9).replace(/(?:\.0*|(\.\d*[1-9])0+)([eE][+-]?\d+)?$/, '$1$2');
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.