-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from concord-consortium/186820341-rounding
New rounding rule
- Loading branch information
Showing
5 changed files
with
48 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { roundForDisplay } from "./math-utils"; | ||
|
||
describe("roundForDisplay", () => { | ||
it("should respect the number of decimal places requested", () => { | ||
expect(roundForDisplay(Math.PI, 4)).toBe(3.142); | ||
expect(roundForDisplay(Math.PI, 3)).toBe(3.14); | ||
expect(roundForDisplay(Math.PI, 2)).toBe(3.1); | ||
expect(roundForDisplay(Math.PI, 1)).toBe(3); | ||
}); | ||
it("should handle very small numbers", () => { | ||
expect(roundForDisplay(.00000039234, 3)).toBe(0.000000392); | ||
expect(roundForDisplay(.00000039234, 2)).toBe(0.00000039); | ||
expect(roundForDisplay(.00000039234, 1)).toBe(0.0000004); | ||
}); | ||
it("should never remove units", () => { | ||
expect(roundForDisplay(123456.1, 3)).toBe(123456); | ||
expect(roundForDisplay(123456.1, 2)).toBe(123456); | ||
expect(roundForDisplay(123456.1, 1)).toBe(123456); | ||
expect(roundForDisplay(17.6, 1)).toBe(18); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Rounds a number in an intuitive way. | ||
* The digits parameter is used as a number of significant digits to maintain for small numbers. | ||
* However, rounding never carries beyond the integer part of a number, even if it contains more than | ||
* that number of significant digits. | ||
* @param x number to round | ||
* @param digits number of digits of precision to maintain in decimals | ||
* @returns a rounded number | ||
*/ | ||
export function roundForDisplay(x: number, digits: number): number { | ||
const intRange = Math.pow(10, digits); | ||
if (x<-intRange || x>intRange) { | ||
return Math.round(x); | ||
} | ||
return +x.toPrecision(digits); | ||
} |