restrictToNDecimals

Benchmark created on


Setup

function prabhat(value, n) {
	let formattedValue = value;
  if (value) {
    const splitDecimals = String(value).split(".");
    if (splitDecimals[1] && splitDecimals[1].length > n) {
      formattedValue = Number(
        `${splitDecimals[0]}.${splitDecimals[1].slice(0, n)}`,
      );
    }
  }
  return formattedValue;
}

function ashish (value, n) {  
  const x = Number(`1${Array(n).fill(0).join('')}`);
  
  return Math.floor(value*x)/x;
}

Test runner

Ready to run.

Testing in
TestOps/sec
prabhat
prabhat(123.123456789, 0)
prabhat(987.987654321, 0)
prabhat(123.123456789, 1)
prabhat(987.987654321, 1)
prabhat(123.123456789, 2)
prabhat(987.987654321, 2)
prabhat(123.123456789, 3)
prabhat(987.987654321, 3)
prabhat(123.123456789, 4)
prabhat(987.987654321, 4)
prabhat(123.123456789, 5)
prabhat(987.987654321, 5)
ready
ashish
ashish(123.123456789, 0)
ashish(987.987654321, 0)
ashish(123.123456789, 1)
ashish(987.987654321, 1)
ashish(123.123456789, 2)
ashish(987.987654321, 2)
ashish(123.123456789, 3)
ashish(987.987654321, 3)
ashish(123.123456789, 4)
ashish(987.987654321, 4)
ashish(123.123456789, 5)
ashish(987.987654321, 5)
ready

Revisions

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