Skip to content

Commit

Permalink
fixed approximate factory to have 2 or less decimal places
Browse files Browse the repository at this point in the history
  • Loading branch information
ernest-okot committed Sep 22, 2017
1 parent e553718 commit 0ec9c1a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/factories/approximate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const TRILLION = 1e12;
const roundNum = (num, length): string =>
(Math.round(num * 10 ** length) / 10 ** length).toFixed(length);

const format = number => roundNum(number, 1);
const format = number => (+roundNum(number, 2)).toString();

const approximate = (number: number): string => {
const absolute = Math.abs(number);
Expand Down
8 changes: 7 additions & 1 deletion test/approximate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const describe = mocha.describe;
describe('approximate', () => {
it('should round off and format values', () => {
const formatted = approximate(6422108692);
expect(formatted).to.equal('6.4bn');
expect(formatted).to.equal('6.42bn');
});
it('should round off to two or less decimal places', () => {
expect(approximate(6422108692)).to.equal('6.42bn');
expect(approximate(6400000000)).to.equal('6.4bn');
expect(approximate(6000000000)).to.equal('6bn');
expect(approximate(0)).to.equal('0');
});
});

0 comments on commit 0ec9c1a

Please sign in to comment.