util: add toFloat function.
This commit is contained in:
parent
46af7f5760
commit
b013b2f0f9
@ -334,8 +334,9 @@ Amount.value = function value(str) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Amount.encode = function encode(value, exp, num) {
|
Amount.encode = function encode(value, exp, num) {
|
||||||
const str = util.toFixed(value, exp);
|
if (num)
|
||||||
return num ? Number(str) : str;
|
return util.toFloat(value, exp);
|
||||||
|
return util.toFixed(value, exp);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -834,7 +834,7 @@ util.memoryUsage = function memoryUsage() {
|
|||||||
* power of ten (uses no floating point arithmetic).
|
* power of ten (uses no floating point arithmetic).
|
||||||
* @param {Number} num
|
* @param {Number} num
|
||||||
* @param {Number} exp - Number of decimal places.
|
* @param {Number} exp - Number of decimal places.
|
||||||
* @returns {String}
|
* @returns {String} Fixed number string.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
util.toFixed = function toFixed(num, exp) {
|
util.toFixed = function toFixed(num, exp) {
|
||||||
@ -870,26 +870,12 @@ util.toFixed = function toFixed(num, exp) {
|
|||||||
return `${sign}${hi}.${lo}`;
|
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
|
* Parse a fixed number string and multiply by a
|
||||||
* power of ten (uses no floating point arithmetic).
|
* power of ten (uses no floating point arithmetic).
|
||||||
* @param {String} str
|
* @param {String} str
|
||||||
* @param {Number} exp - Number of decimal places.
|
* @param {Number} exp - Number of decimal places.
|
||||||
* @returns {Number}
|
* @returns {Number} Integer.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
util.fromFixed = function fromFixed(str, exp) {
|
util.fromFixed = function fromFixed(str, exp) {
|
||||||
@ -947,6 +933,32 @@ util.fromFixed = function fromFixed(str, exp) {
|
|||||||
return sign * (hi * mult + lo);
|
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
|
* Helpers
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -72,31 +72,17 @@ describe('Utils', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should convert satoshi to btc', () => {
|
it('should convert satoshi to btc', () => {
|
||||||
let btc = Amount.btc(5460);
|
assert.strictEqual(Amount.btc(5460), '0.0000546');
|
||||||
assert.strictEqual(btc, '0.0000546');
|
assert.strictEqual(Amount.btc(54678 * 1000000), '546.78');
|
||||||
|
assert.strictEqual(Amount.btc(5460 * 10000000), '546.0');
|
||||||
btc = Amount.btc(54678 * 1000000);
|
|
||||||
assert.strictEqual(btc, '546.78');
|
|
||||||
|
|
||||||
btc = Amount.btc(5460 * 10000000);
|
|
||||||
assert.strictEqual(btc, '546.0');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should convert btc to satoshi', () => {
|
it('should convert btc to satoshi', () => {
|
||||||
let btc = Amount.value('0.0000546');
|
assert.strictEqual(Amount.value('0.0000546'), 5460);
|
||||||
assert.strictEqual(btc, 5460);
|
assert.strictEqual(Amount.value('546.78'), 54678 * 1000000);
|
||||||
|
assert.strictEqual(Amount.value('546'), 5460 * 10000000);
|
||||||
btc = Amount.value('546.78');
|
assert.strictEqual(Amount.value('546.0'), 5460 * 10000000);
|
||||||
assert.strictEqual(btc, 54678 * 1000000);
|
assert.strictEqual(Amount.value('546.0000'), 5460 * 10000000);
|
||||||
|
|
||||||
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.doesNotThrow(() => {
|
assert.doesNotThrow(() => {
|
||||||
Amount.value('546.00000000000000000');
|
Amount.value('546.00000000000000000');
|
||||||
@ -122,10 +108,14 @@ describe('Utils', function() {
|
|||||||
Amount.value('190071992.54740991');
|
Amount.value('190071992.54740991');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
assert.strictEqual(0.15645647 * 1e8, 15645646.999999998);
|
||||||
assert.strictEqual(parseFloat('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.fromFixed('0.15645647', 8), 15645647);
|
||||||
assert.strictEqual(util.toFixed(15645647, 8), '0.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', () => {
|
it('should write/read new varints', () => {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user