decimal-js icon indicating copy to clipboard operation
decimal-js copied to clipboard

0.92 - 1 = 0.-8 ???

Open Phara0h opened this issue 10 years ago • 1 comments

I am having the issue when I am doing Decimal(0.92).sub(1).

Decimal(0.92).sub(1) == "0.-8"

when it should equal

Decimal(0.92).sub(1) == "-0.8"

Phara0h avatar May 30 '14 18:05 Phara0h

just hit same error, but my solution is different:

   var neg_exp = function(str, position) {
        position = Math.abs(position);

        var sign = '';

        if (str.charAt(0) === '-') {
          sign = '-';
          str = str.substr(1);
        }

        var offset = position - str.length;
        var sep = INTERNAL_DECIMAL_SEPARATOR;

        if(offset >= 0) {
            str = zero(offset) + str;
            sep = '0' + DECIMAL_SEPARATOR;
        }

        var length = str.length;
        var head = str.substr(0, length - position);
        var tail = str.substring(length  - position, length);
        return sign + head + sep + tail;
    };

jerzyk avatar Dec 20 '16 07:12 jerzyk