Made to/fromWei return strings instead of numbers

This commit is contained in:
Fabian Vogelsteller 2015-02-20 12:40:09 +01:00
parent ed7d473efd
commit 91c015e402
8 changed files with 71 additions and 15 deletions

View File

@ -30,6 +30,11 @@
"name": "Marian Oancea",
"email": "marian@ethdev.com",
"homepage": "https://github.com/cubedro"
},
{
"name": "Fabian Vogelsteller",
"email": "fabian@ethdev.com",
"homepage": "https://github.com/frozeman"
}
],
"license": "LGPL-3.0",

13
dist/ethereum.js vendored
View File

@ -1555,6 +1555,7 @@ var toEth = function (str) {
return s + ' ' + units[unit];
};
var toDecimal = function (val) {
// remove 0x and place 0, if it's required
val = val.length > 2 ? val.substring(2) : "0";
@ -1579,9 +1580,9 @@ Possible units are:
- tether
@method fromWei
@param {Number|String} number can be a number or a HEX of a decimal
@param {Number|String} number can be a number, number string or a HEX of a decimal
@param {String} unit the unit to convert to
@return {Number|Object} When given a BigNumber object it returns one as well, otherwise a number
@return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
*/
var fromWei = function(number, unit) {
var isBigNumber = true;
@ -1638,7 +1639,7 @@ var fromWei = function(number, unit) {
break;
}
return (isBigNumber) ? number : number.toNumber();
return (isBigNumber) ? number : number.toString(10);
};
/**
@ -1658,9 +1659,9 @@ Possible units are:
- tether
@method toWei
@param {Number|String} number can be a number or a HEX of a decimal
@param {Number|String} number can be a number, number string or a HEX of a decimal
@param {String} unit the unit to convert to
@return {Number|Object} When given a BigNumber object it returns one as well, otherwise a number
@return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
*/
var toWei = function(number, unit) {
var isBigNumber = true;
@ -1717,7 +1718,7 @@ var toWei = function(number, unit) {
break;
}
return (isBigNumber) ? number : number.toNumber();
return (isBigNumber) ? number : number.toString(10);
};

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -136,6 +136,7 @@ var toEth = function (str) {
return s + ' ' + units[unit];
};
var toDecimal = function (val) {
// remove 0x and place 0, if it's required
val = val.length > 2 ? val.substring(2) : "0";
@ -160,9 +161,9 @@ Possible units are:
- tether
@method fromWei
@param {Number|String} number can be a number or a HEX of a decimal
@param {Number|String} number can be a number, number string or a HEX of a decimal
@param {String} unit the unit to convert to
@return {Number|Object} When given a BigNumber object it returns one as well, otherwise a number
@return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
*/
var fromWei = function(number, unit) {
var isBigNumber = true;
@ -219,7 +220,7 @@ var fromWei = function(number, unit) {
break;
}
return (isBigNumber) ? number : number.toNumber();
return (isBigNumber) ? number : number.toString(10);
};
/**
@ -239,9 +240,9 @@ Possible units are:
- tether
@method toWei
@param {Number|String} number can be a number or a HEX of a decimal
@param {Number|String} number can be a number, number string or a HEX of a decimal
@param {String} unit the unit to convert to
@return {Number|Object} When given a BigNumber object it returns one as well, otherwise a number
@return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
*/
var toWei = function(number, unit) {
var isBigNumber = true;
@ -298,7 +299,7 @@ var toWei = function(number, unit) {
break;
}
return (isBigNumber) ? number : number.toNumber();
return (isBigNumber) ? number : number.toString(10);
};

View File

@ -67,6 +67,11 @@
"name": "Marian Oancea",
"email": "marian@ethdev.com",
"url": "https://github.com/cubedro"
},
{
"name": "Fabian Vogelsteller",
"email": "fabian@ethdev.com",
"homepage": "https://github.com/frozeman"
}
],
"license": "LGPL-3.0"

22
test/utils.fromWei.js Normal file
View File

@ -0,0 +1,22 @@
var assert = require('assert');
var utils = require('../lib/utils.js');
describe('utils', function () {
describe('toWei', function () {
it('should return the correct value', function () {
assert.equal(EthTools.fromWei(1000000000000000000, 'wei'), '1000000000000000000');
assert.equal(EthTools.fromWei(1000000000000000000, 'kwei'), '1000000000000000');
assert.equal(EthTools.fromWei(1000000000000000000, 'mwei'), '1000000000000');
assert.equal(EthTools.fromWei(1000000000000000000, 'gwei'), '1000000000');
assert.equal(EthTools.fromWei(1000000000000000000, 'szabo'), '1000000');
assert.equal(EthTools.fromWei(1000000000000000000, 'finney'), '1000');
assert.equal(EthTools.fromWei(1000000000000000000, 'ether'), '1');
assert.equal(EthTools.fromWei(1000000000000000000, 'kether'), '0.001');
assert.equal(EthTools.fromWei(1000000000000000000, 'grand'), '0.001');
assert.equal(EthTools.fromWei(1000000000000000000, 'mether'), '0.000001');
assert.equal(EthTools.fromWei(1000000000000000000, 'gether'), '0.000000001');
assert.equal(EthTools.fromWei(1000000000000000000, 'tether'), '0.000000000001');
});
});
});

22
test/utils.toWei.js Normal file
View File

@ -0,0 +1,22 @@
var assert = require('assert');
var utils = require('../lib/utils.js');
describe('utils', function () {
describe('toWei', function () {
it('should return the correct value', function () {
assert.equal(EthTools.toWei(1, 'wei'), '1');
assert.equal(EthTools.toWei(1, 'kwei'), '1000');
assert.equal(EthTools.toWei(1, 'mwei'), '1000000');
assert.equal(EthTools.toWei(1, 'gwei'), '1000000000');
assert.equal(EthTools.toWei(1, 'szabo'), '1000000000000');
assert.equal(EthTools.toWei(1, 'finney'), '1000000000000000');
assert.equal(EthTools.toWei(1, 'ether'), '1000000000000000000');
assert.equal(EthTools.toWei(1, 'kether'), '1000000000000000000000');
assert.equal(EthTools.toWei(1, 'grand'), '1000000000000000000000');
assert.equal(EthTools.toWei(1, 'mether'), '1000000000000000000000000');
assert.equal(EthTools.toWei(1, 'gether'), '1000000000000000000000000000');
assert.equal(EthTools.toWei(1, 'tether'), '1000000000000000000000000000000');
});
});
});