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

Enhance helper.decimalPlaces #5992

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/core/core.helpers.js
Expand Up @@ -126,7 +126,7 @@ module.exports = function() {
};
helpers.almostWhole = function(x, epsilon) {
var rounded = Math.round(x);
return (((rounded - epsilon) < x) && ((rounded + epsilon) > x));
return (((rounded - epsilon) <= x) && ((rounded + epsilon) >= x));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems reasonable.

};
helpers.max = function(array) {
return array.reduce(function(max, value) {
Expand Down Expand Up @@ -185,11 +185,13 @@ module.exports = function() {
if (!helpers.isFinite(x)) {
return;
}
var e = 1;
x = Math.abs(x);
var p = 0;
while (Math.round(x * e) / e !== x) {
e *= 10;
p++;
if (x > 0) {
while (x < 0.99 || !helpers.almostWhole(x, 1e-6)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This however fails with large numbers, for example (current version works with these):

		expect(helpers.decimalPlaces(12345678.1234)).toBe(4); // this PR results to 9
		expect(helpers.decimalPlaces(1234567890.1234567)).toBe(7); // this PR results to 6

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are good tests and I agree the result is not acceptable. I will review...

x *= 10;
p++;
}
}
return p;
};
Expand Down
5 changes: 5 additions & 0 deletions test/specs/core.helpers.tests.js
Expand Up @@ -212,6 +212,8 @@ describe('Core helper tests', function() {
it('should correctly determine if a numbers are essentially whole', function() {
expect(helpers.almostWhole(0.99999, 0.0001)).toBe(true);
expect(helpers.almostWhole(0.9, 0.0001)).toBe(false);
expect(helpers.almostWhole(1234567890123, 0.0001)).toBe(true);
expect(helpers.almostWhole(1234567890123.001, 0.0001)).toBe(false);
});

it('should generate integer ids', function() {
Expand Down Expand Up @@ -244,6 +246,9 @@ describe('Core helper tests', function() {
expect(helpers.decimalPlaces(0)).toBe(0);
expect(helpers.decimalPlaces(0.01)).toBe(2);
expect(helpers.decimalPlaces(-0.01)).toBe(2);
expect(helpers.decimalPlaces(0.2 + 0.1)).toBe(1);
expect(helpers.decimalPlaces(0.12 + 0.02)).toBe(2);
expect(helpers.decimalPlaces(3.1415e-34)).toBe(38);
expect(helpers.decimalPlaces('1')).toBe(undefined);
expect(helpers.decimalPlaces('')).toBe(undefined);
expect(helpers.decimalPlaces(undefined)).toBe(undefined);
Expand Down