Example #1
0
function Num(num, prec, isNumInt) {
    if (!(this instanceof Num)) {
        return new Num(num, prec, isNumInt);
    }

    var self = this;
    if (num instanceof Num) {
        self._int = int(num._int);
        self._precision = num._precision;
        return self;
    }

    // convert to a string
    num = '' + num;

    var currentPrecision = 0;

    // find Num point
    var dec = num.indexOf('.');
    num = num.replace('.', '');

    if (dec >= 0)
    {
        currentPrecision = num.length - dec;
    }

    if (isNaN(prec))
    {
        this._precision = currentPrecision;
    }
    else
    {
        // Pad with zero as needed if it's not an internal int
        if (isNumInt !== true)
        {
            if (prec > currentPrecision)
            {
                var zeroWidth = prec - currentPrecision + 1;
                if (zeroWidth > 1)
                {
                    num = num + new Array(zeroWidth).join('0');
                }
            }
            else
            {
                // Trim
                num = num.substr(0, num.length - (currentPrecision - prec));
            }
        }

        this._precision = prec;
    }

    this._int = int(num);
}
Example #2
0
Num.prototype.set_precision = function(precision) {
    var self = this;
    var precision_diff = precision - self._precision;

    if (precision_diff > 0) {
        self._int = self._int.mul(int(10).pow(precision_diff));
    }
    else if(precision_diff < 0) {
        self._int = self._int.div(int(10).pow(-precision_diff));
    }

    self._precision += precision_diff;
    return self;
};
Example #3
0
function Num(num, prec) {
    if (!(this instanceof Num)) {
        return new Num(num, prec);
    }

    var self = this;
    if (num instanceof Num) {
        self._int = int(num._int);
        self._precision = num._precision;
        return self;
    }

    var precision = 0;

    num = num || 0;
    // convert to a string
    num = '' + num;

    var idx_e = num.indexOf('e');
    if (idx_e > 0) {
        var exp = num.slice(idx_e + 1)
        num = num.slice(0, idx_e)
        precision = -exp;
    }

    // find Num point
    var dec = num.indexOf('.');

    if (dec >= 0) {
        // take out the Num point
        num = num.replace('.', '');
        precision = precision + num.length - dec;
    }

    self._int = int(num);
    self._precision = prec || precision;
    self._nan = self._int._nan;
}
Example #4
0
/// decode a base58 string payload into a hex representation
function decode(payload) {
    var base = 58;

    var length = payload.length;
    var num = int(0);
    var leading_zero = 0;
    var seen_other = false;
    for (var i=0; i<length ; ++i) {
        var char = payload[i];
        var p = positions[char];

        // if we encounter an invalid character, decoding fails
        if (p === undefined) {
            throw new Error('invalid base58 string: ' + payload);
        }

        num = num.mul(base).add(p);

        if (char == '1' && !seen_other) {
            ++leading_zero;
        }
        else {
            seen_other = true;
        }
    }

    var hex = num.toString(16);

    // num.toString(16) does not have leading 0
    if (hex.length % 2 !== 0) {
        hex = '0' + hex;
    }

    // strings starting with only ones need to be adjusted
    // e.g. '1' should map to '00' and not '0000'
    if (leading_zero && !seen_other) {
      --leading_zero;
    }

    while (leading_zero-- > 0) {
        hex = '00' + hex;
    }

    return hex;
}
Example #5
0
function convertChromiumTimestampToUnix(timestamp) {

	return int(timestamp.toString()).sub('11644473600000000').div(1000000);

}