Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #293 from isocolsky/fix/format-amount
Browse files Browse the repository at this point in the history
Fix/format amount
  • Loading branch information
matiu committed Jun 23, 2016
2 parents e1bb8d7 + f8ca6d1 commit 94b5633
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 46 deletions.
20 changes: 16 additions & 4 deletions lib/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@ Constants.BIP45_SHARED_INDEX = 0x80000000 - 1;
Constants.UNITS = {
btc: {
toSatoshis: 100000000,
maxDecimals: 6,
minDecimals: 2,
full: {
maxDecimals: 8,
minDecimals: 8,
},
short: {
maxDecimals: 6,
minDecimals: 2,
}
},
bit: {
toSatoshis: 100,
maxDecimals: 0,
minDecimals: 0,
full: {
maxDecimals: 2,
minDecimals: 2,
},
short: {
maxDecimals: 0,
minDecimals: 0,
}
},
};

Expand Down
14 changes: 10 additions & 4 deletions lib/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ var Defaults = require('./defaults');

function Utils() {};

Utils.SJCL = {
};
Utils.SJCL = {};

Utils.encryptMessage = function(message, encryptingKey) {
var key = sjcl.codec.base64.toBits(encryptingKey);
Expand Down Expand Up @@ -143,6 +142,11 @@ Utils.formatAmount = function(satoshis, unit, opts) {
$.shouldBeNumber(satoshis);
$.checkArgument(_.contains(_.keys(Constants.UNITS), unit));

function roundDown(number, decimals) {
var exp = Math.pow(10, decimals || 0);
return (Math.floor(number * exp) / exp);
}

function addSeparators(nStr, thousands, decimal, minDecimals) {
nStr = nStr.replace('.', decimal);
var x = nStr.split(decimal);
Expand All @@ -161,8 +165,10 @@ Utils.formatAmount = function(satoshis, unit, opts) {
opts = opts || {};

var u = Constants.UNITS[unit];
var amount = (satoshis / u.toSatoshis).toFixed(u.maxDecimals);
return addSeparators(amount, opts.thousandsSeparator || ',', opts.decimalSeparator || '.', u.minDecimals);
var precision = opts.fullPrecision ? 'full' : 'short';

var amount = roundDown((satoshis / u.toSatoshis), u[precision].maxDecimals).toFixed(u[precision].maxDecimals);
return addSeparators(amount, opts.thousandsSeparator || ',', opts.decimalSeparator || '.', u[precision].minDecimals);
};

Utils.buildTx = function(txp) {
Expand Down
39 changes: 9 additions & 30 deletions test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4947,46 +4947,25 @@ describe('client API', function() {
var cases = [{
args: [1, 'bit'],
expected: '0',
}, {
args: [1, 'bit', {
fullPrecision: true
}],
expected: '0.01',
}, {
args: [1, 'btc'],
expected: '0.00',
}, {
args: [0, 'bit'],
expected: '0',
}, {
args: [12345678, 'bit'],
expected: '123,457',
}, {
args: [12345678, 'btc'],
expected: '0.123457',
}, {
args: [12345611, 'btc'],
expected: '0.123456',
}, {
args: [1234, 'btc'],
expected: '0.000012',
}, {
args: [1299, 'btc'],
expected: '0.000013',
}, {
args: [1234567899999, 'btc'],
expected: '12,345.679',
}, {
args: [12345678, 'bit', {
thousandsSeparator: '.'
}],
expected: '123.457',
}, {
args: [12345678, 'btc', {
decimalSeparator: ','
args: [1, 'btc', {
fullPrecision: true
}],
expected: '0,123457',
expected: '0.00000001',
}, {
args: [1234567899999, 'btc', {
thousandsSeparator: ' ',
decimalSeparator: ','
}],
expected: '12 345,679',
expected: '12 345,678999',
}, ];

_.each(cases, function(testCase) {
Expand Down
71 changes: 63 additions & 8 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('Utils', function() {
});

describe('#formatAmount', function() {
it('should successfully format amount', function() {
it('should successfully format short amount', function() {
var cases = [{
args: [1, 'bit'],
expected: '0',
Expand All @@ -65,10 +65,10 @@ describe('Utils', function() {
expected: '0',
}, {
args: [12345678, 'bit'],
expected: '123,457',
expected: '123,456',
}, {
args: [12345678, 'btc'],
expected: '0.123457',
expected: '0.123456',
}, {
args: [12345611, 'btc'],
expected: '0.123456',
Expand All @@ -77,29 +77,84 @@ describe('Utils', function() {
expected: '0.000012',
}, {
args: [1299, 'btc'],
expected: '0.000013',
expected: '0.000012',
}, {
args: [1234567899999, 'btc'],
expected: '12,345.679',
expected: '12,345.678999',
}, {
args: [12345678, 'bit', {
thousandsSeparator: '.'
}],
expected: '123.457',
expected: '123.456',
}, {
args: [12345678, 'btc', {
decimalSeparator: ','
}],
expected: '0,123456',
}, {
args: [1234567899999, 'btc', {
thousandsSeparator: ' ',
decimalSeparator: ','
}],
expected: '12 345,678999',
}, ];

_.each(cases, function(testCase) {
Utils.formatAmount.apply(this, testCase.args).should.equal(testCase.expected);
});
});
it('should successfully format full amount', function() {
var cases = [{
args: [1, 'bit'],
expected: '0.01',
}, {
args: [1, 'btc'],
expected: '0.00000001',
}, {
args: [0, 'bit'],
expected: '0.00',
}, {
args: [12345678, 'bit'],
expected: '123,456.78',
}, {
args: [12345678, 'btc'],
expected: '0.12345678',
}, {
args: [1234567, 'btc'],
expected: '0.01234567',
}, {
args: [12345611, 'btc'],
expected: '0.12345611',
}, {
args: [1234, 'btc'],
expected: '0.00001234',
}, {
args: [1299, 'btc'],
expected: '0.00001299',
}, {
args: [1234567899999, 'btc'],
expected: '12,345.67899999',
}, {
args: [12345678, 'bit', {
thousandsSeparator: "'"
}],
expected: "123'456.78",
}, {
args: [12345678, 'btc', {
decimalSeparator: ','
}],
expected: '0,123457',
expected: '0,12345678',
}, {
args: [1234567899999, 'btc', {
thousandsSeparator: ' ',
decimalSeparator: ','
}],
expected: '12 345,679',
expected: '12 345,67899999',
}, ];

_.each(cases, function(testCase) {
testCase.args[2] = testCase.args[2] || {};
testCase.args[2].fullPrecision = true;
Utils.formatAmount.apply(this, testCase.args).should.equal(testCase.expected);
});
});
Expand Down

0 comments on commit 94b5633

Please sign in to comment.