utils: toBTC()
This commit is contained in:
parent
2d937b67a1
commit
7e72a4d79d
@ -365,9 +365,23 @@ utils.revHex = function revHex(s) {
|
||||
utils.assert = function assert(val, msg) {
|
||||
if (!val)
|
||||
throw new Error(msg || 'Assertion failed');
|
||||
}
|
||||
};
|
||||
|
||||
utils.assert.equal = function assertEqual(l, r, msg) {
|
||||
if (l != r)
|
||||
throw new Error(msg || ('Assertion failed: ' + l + ' != ' + r));
|
||||
}
|
||||
};
|
||||
|
||||
utils.toBTC = function toBTC(satoshi) {
|
||||
var m = new bn(10000000).mul(new bn(10));
|
||||
var lo = satoshi.mod(m);
|
||||
if (lo.cmpn(0) !== 0) {
|
||||
lo = lo.toString(10);
|
||||
while (lo.length < 8)
|
||||
lo = '0' + lo;
|
||||
lo = '.' + lo;
|
||||
} else {
|
||||
lo = '';
|
||||
}
|
||||
return satoshi.div(m).toString(10) + lo.replace(/0+$/, '');
|
||||
};
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
var assert = require('assert');
|
||||
var bn = require('bn.js');
|
||||
var bcoin = require('../');
|
||||
|
||||
describe('Utils', function() {
|
||||
@ -18,4 +19,13 @@ describe('Utils', function() {
|
||||
var target = bcoin.utils.bitsToTarget(bits);
|
||||
assert(bcoin.utils.testTarget(target, hash));
|
||||
});
|
||||
|
||||
it('should convert satoshi to btc', function() {
|
||||
var btc = bcoin.utils.toBTC(new bn(5460));
|
||||
assert.equal(btc, '0.0000546');
|
||||
var btc = bcoin.utils.toBTC(new bn(54678).mul(new bn(1000000)));
|
||||
assert.equal(btc, '546.78');
|
||||
var btc = bcoin.utils.toBTC(new bn(5460).mul(new bn(10000000)));
|
||||
assert.equal(btc, '546');
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user