Support Numbers in formatValue

This commit is contained in:
Nadav Ivgi 2013-11-24 05:11:56 +02:00
parent 397de49089
commit df88e93849

View file

@ -67,7 +67,7 @@ module.exports = {
/** /**
* Format a Bitcoin value as a string. * Format a Bitcoin value as a string.
* *
* Takes a BigInteger or byte-array and returns that amount of Bitcoins in a * Takes a Number, BigInteger or byte-array and returns that amount of Bitcoins in a
* nice standard formatting. * nice standard formatting.
* *
* Examples: * Examples:
@ -76,14 +76,20 @@ module.exports = {
* 900.99998888 * 900.99998888
* 34.00 * 34.00
*/ */
formatValue: function (valueBuffer) { formatValue: function (value) {
var value = this.valueToBigInt(valueBuffer).toString(); if (typeof value == 'number') {
var integerPart = value.length > 8 ? value.substr(0, value.length-8) : '0'; value = value/100000000;
var decimalPart = value.length > 8 ? value.substr(value.length-8) : value; // at least two decimal places
while (decimalPart.length < 8) decimalPart = "0"+decimalPart; return Math.floor(value*10)==value*10 ? value.toFixed(2) : ''+value;
decimalPart = decimalPart.replace(/0*$/, ''); } else {
while (decimalPart.length < 2) decimalPart += "0"; value = module.exports.valueToBigInt(value).toString();
return integerPart+"."+decimalPart; var integerPart = value.length > 8 ? value.substr(0, value.length-8) : '0';
var decimalPart = value.length > 8 ? value.substr(value.length-8) : value;
while (decimalPart.length < 8) decimalPart = "0"+decimalPart;
decimalPart = decimalPart.replace(/0*$/, '');
while (decimalPart.length < 2) decimalPart += "0";
return integerPart+"."+decimalPart;
}
}, },
/** /**