util: add toFloat function.

This commit is contained in:
Christopher Jeffrey 2017-08-13 13:15:56 -07:00
parent 46af7f5760
commit b013b2f0f9
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 44 additions and 41 deletions

View File

@ -334,8 +334,9 @@ Amount.value = function value(str) {
*/
Amount.encode = function encode(value, exp, num) {
const str = util.toFixed(value, exp);
return num ? Number(str) : str;
if (num)
return util.toFloat(value, exp);
return util.toFixed(value, exp);
};
/**

View File

@ -834,7 +834,7 @@ util.memoryUsage = function memoryUsage() {
* power of ten (uses no floating point arithmetic).
* @param {Number} num
* @param {Number} exp - Number of decimal places.
* @returns {String}
* @returns {String} Fixed number string.
*/
util.toFixed = function toFixed(num, exp) {
@ -870,26 +870,12 @@ util.toFixed = function toFixed(num, exp) {
return `${sign}${hi}.${lo}`;
};
/**
* Parse a double float number and multiply by a
* power of ten (uses no floating point arithmetic).
* @param {Number} num
* @param {Number} exp - Number of decimal places.
* @returns {Number}
*/
util.fromFloat = function fromFloat(num, exp) {
assert(typeof num === 'number' && isFinite(num));
assert(Number.isSafeInteger(exp));
return util.fromFixed(num.toFixed(exp), exp);
};
/**
* Parse a fixed number string and multiply by a
* power of ten (uses no floating point arithmetic).
* @param {String} str
* @param {Number} exp - Number of decimal places.
* @returns {Number}
* @returns {Number} Integer.
*/
util.fromFixed = function fromFixed(str, exp) {
@ -947,6 +933,32 @@ util.fromFixed = function fromFixed(str, exp) {
return sign * (hi * mult + lo);
};
/**
* Convert int to float and reduce by a power
* of ten (uses no floating point arithmetic).
* @param {Number} num
* @param {Number} exp - Number of decimal places.
* @returns {Number} Double float.
*/
util.toFloat = function toFloat(num, exp) {
return Number(util.toFixed(num, exp));
};
/**
* Parse a double float number and multiply by a
* power of ten (uses no floating point arithmetic).
* @param {Number} num
* @param {Number} exp - Number of decimal places.
* @returns {Number} Integer.
*/
util.fromFloat = function fromFloat(num, exp) {
assert(typeof num === 'number' && isFinite(num));
assert(Number.isSafeInteger(exp));
return util.fromFixed(num.toFixed(exp), exp);
};
/*
* Helpers
*/

View File

@ -72,31 +72,17 @@ describe('Utils', function() {
});
it('should convert satoshi to btc', () => {
let btc = Amount.btc(5460);
assert.strictEqual(btc, '0.0000546');
btc = Amount.btc(54678 * 1000000);
assert.strictEqual(btc, '546.78');
btc = Amount.btc(5460 * 10000000);
assert.strictEqual(btc, '546.0');
assert.strictEqual(Amount.btc(5460), '0.0000546');
assert.strictEqual(Amount.btc(54678 * 1000000), '546.78');
assert.strictEqual(Amount.btc(5460 * 10000000), '546.0');
});
it('should convert btc to satoshi', () => {
let btc = Amount.value('0.0000546');
assert.strictEqual(btc, 5460);
btc = Amount.value('546.78');
assert.strictEqual(btc, 54678 * 1000000);
btc = Amount.value('546');
assert.strictEqual(btc, 5460 * 10000000);
btc = Amount.value('546.0');
assert.strictEqual(btc, 5460 * 10000000);
btc = Amount.value('546.0000');
assert.strictEqual(btc, 5460 * 10000000);
assert.strictEqual(Amount.value('0.0000546'), 5460);
assert.strictEqual(Amount.value('546.78'), 54678 * 1000000);
assert.strictEqual(Amount.value('546'), 5460 * 10000000);
assert.strictEqual(Amount.value('546.0'), 5460 * 10000000);
assert.strictEqual(Amount.value('546.0000'), 5460 * 10000000);
assert.doesNotThrow(() => {
Amount.value('546.00000000000000000');
@ -122,10 +108,14 @@ describe('Utils', function() {
Amount.value('190071992.54740991');
});
assert.strictEqual(0.15645647 * 1e8, 15645646.999999998);
assert.strictEqual(parseFloat('0.15645647') * 1e8, 15645646.999999998);
assert.strictEqual(util.fromFloat(0.15645647, 8), 15645647);
assert.strictEqual(15645647 / 1e8, 0.15645647);
assert.strictEqual(util.fromFixed('0.15645647', 8), 15645647);
assert.strictEqual(util.toFixed(15645647, 8), '0.15645647');
assert.strictEqual(util.fromFloat(0.15645647, 8), 15645647);
assert.strictEqual(util.toFloat(15645647, 8), 0.15645647);
});
it('should write/read new varints', () => {