Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add an execution test for paren_remover #8371

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function toFixed(value, maxDecimals, roundingFunction, optionals) {
var splitValue = value.toString().split("."),
minDecimals = maxDecimals - (optionals || 0),
optionalsRegExp,
power,
output;
var boundedPrecisions;
// var unused = 'xxxx';
// Use the smallest precision value possible to avoid errors from floating point representation
if (splitValue.length === 2) {
boundedPrecisions = Math.min(
Math.max(splitValue[1].length, minDecimals),
maxDecimals
);
} else {
boundedPrecisions = minDecimals;
}
power = Math.pow(10, boundedPrecisions);
// Multiply up by precision, round accurately, then divide and use native toFixed():
output = (roundingFunction(value + "e+" + boundedPrecisions) / power).toFixed(
boundedPrecisions
);
if (optionals > maxDecimals - boundedPrecisions) {
optionalsRegExp = new RegExp(
"\\.?0{1," + (optionals - (maxDecimals - boundedPrecisions)) + "}$"
);
output = output.replace(optionalsRegExp, "");
}
return output;
}

it("should work", () => {
expect(toFixed(1.2345, 2, Math.round, 1)).toBe("1.23");
});
Loading