diff --git a/src/formatter.js b/src/formatter.js index cfdaef08..bc884ce5 100755 --- a/src/formatter.js +++ b/src/formatter.js @@ -155,7 +155,7 @@ module.exports = steemAPI => { } return { - reputation: function(reputation) { + reputation: function(reputation, decimal_places = 0) { if (reputation == 0) return 25; if (!reputation) return reputation; let neg = reputation < 0; @@ -163,7 +163,11 @@ module.exports = steemAPI => { rep = neg ? rep.substring(1) : rep; let v = (Math.log10((rep > 0 ? rep : -rep) - 10) - 9); v = neg ? -v : v; - return parseInt(v * 9 + 25); + v = v * 9 + 25; + if (decimal_places > 0) { + return +(Math.round(v + "e+" + decimal_places) + "e-" + decimal_places); + } + return parseInt(v); }, vestToSteem: function( diff --git a/test/reputation.test.js b/test/reputation.test.js index 244a9aad..66c8ce43 100644 --- a/test/reputation.test.js +++ b/test/reputation.test.js @@ -32,5 +32,37 @@ describe('steem.format.reputation', ()=> { }); it('rep -22233344455 => 12', () => { assert.equal(reputation(-22233344455), 12); - }); + }); + + // with decimal places + it('rep 0 => 25', () => { + assert.equal(reputation(0, 1), 25); + }); + it('rep 95832978796820 => 69.83', () => { + assert.equal(reputation(95832978796820, 2), 69.83); + }); + it('rep 10004392664120 => 61.002', () => { + assert.equal(reputation(10004392664120, 3), 61.002); + }); + it('rep 30999525306309 => 65.4222', () => { + assert.equal(reputation(30999525306309, 4), 65.4222); + }); + it('rep -37765258368568 => -16.19383', () => { + assert.equal(reputation(-37765258368568, 5), -16.19383); + }); + it('rep 334486135407077 => 74.719403', () => { + assert.equal(reputation(334486135407077, 6), 74.719403); + }); + it('rep null => null', () => { + assert.equal(reputation(null, 7), null); + }); + it('rep undefined => undefined', () => { + assert.equal(reputation(undefined, 8), undefined); + }); + it('rep -1234123412342234 => -29.822227322', () => { + assert.equal(reputation(-1234123412342234, 9), -29.822227322); + }); + it('rep -22233344455 => 12.8769568338', () => { + assert.equal(reputation(-22233344455, 10), 12.8769568338); + }); })