Hum I don't think we should change the sign of the provided number if it is already rounded.
But when we need to round a number, I agree that it should NEVER returns -0.
This is wrong anyway:
https://github.com/sindresorhus/round-to/blob/6885adef26882b186704e4c8fc414b785947bb92/test.js#L27
This is my proposal:
test('roundTo()', t => {
t.is(roundTo(0.129, 3), 0.129);
t.is(roundTo(0.129, 2), 0.13);
t.is(roundTo(0.129, 1), 0.1);
t.is(roundTo(1.005, 2), 1.01);
t.is(roundTo(1.005, 0), 1);
t.is(roundTo(111.1, -2), 100);
t.is(roundTo(-0.375, 2), -0.38);
t.false(Number.isNaN(roundTo(10_000_000_000_000, 8)));
t.is(roundTo(0.375_423_234_234_234_324_324_324_324_32, 8), 0.375_423_23); // eslint-disable-line no-loss-of-precision
t.is(roundTo(0.123_178_263_8, Number.POSITIVE_INFINITY), 0.123_178_263_8);
t.is(roundTo(0.5, 0), 1);
t.is(roundTo(-0.5, 0), -1);
t.is(roundTo(5.12, 1), 5.1);
t.is(roundTo(-5.12, 1), -5.1);
t.is(roundTo(1.005, 2), 1.01);
t.is(roundTo(-1.005, 2), -1.01);
t.is(roundTo(39.425, 2), 39.43);
t.is(roundTo(-39.425, 2), -39.43);
t.is(roundTo(1262.48, -1), 1260);
t.is(roundTo(1262.48, -2), 1300);
t.is(roundTo(0.597 / 6, 3), 0.1);
t.is(roundTo(6.578_040_334_659_363e-10, 6), 0);
- t.is(roundTo(-6.578_040_334_659_363e-10, 6), -0);
+ t.is(roundTo(-6.578_040_334_659_363e-10, 6), 0);
t.is(roundTo(0, 1), 0);
- t.is(roundTo(-0, 1), 0);
+ t.is(roundTo(-0, 1), -0);
});
Does that make sense?