Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions BigRational.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
decPart = decPart.slice(0, -1);
}
}
if(digits < 1) decPart = "";
if(digits < 1) decPart = "";
if (this.isNegative()) {
intPart = "-"+intPart;
}
Expand All @@ -186,9 +186,9 @@
};

BigRational.prototype.valueOf = function () {
if(!isFinite(+this.num) || !isFinite(+this.denom)) {
return +this.toDecimal(64);
}
if(!isFinite(+this.num) || !isFinite(+this.denom)) {
return +this.toDecimal(64);
}
return this.num / this.denom;
};

Expand Down Expand Up @@ -238,6 +238,18 @@
return new BigRational(bigInt(n), bigInt[1]);
}
function parse(a, b) {
if(
a!==null
&& !(a instanceof BigRational)
&& ! bigInt.isInstance(a)

&& a instanceof Object
&& typeof a === 'object'
)
{
b = a["denom"] || a["denominator"];
a = a["num"] || a["numerator"];
}
if(!a) {
return new BigRational(bigInt(0), bigInt[1]);
}
Expand All @@ -253,6 +265,15 @@
var denom;

var text = String(a);
var percents = text.split("%");
if(percents.length>2){
throw new Error("Invalid input: too many '%' tokens");
}
if(percents.length>1){
text = percents[0];
var percent = true;
}

var texts = text.split("/");
if(texts.length > 2) {
throw new Error("Invalid input: too many '/' tokens");
Expand All @@ -271,11 +292,11 @@
num = num.subtract(parts[1]);
}
denom = bigInt(texts[1]);
return reduce(num, denom);
return (percent) ? reduce(num, denom).divide(bigRat('100')) : reduce(num, denom);
}
return reduce(bigInt(texts[0]), bigInt(texts[1]));
return (percent) ? reduce(bigInt(texts[0]), bigInt(texts[1])).divide(bigRat('100')) : reduce(bigInt(texts[0]), bigInt(texts[1]));
}
return parseDecimal(text);
return (percent) ? parseDecimal(text).divide(bigRat('100')) : parseDecimal(text);
}

parse.zero = parse(0);
Expand Down
4 changes: 2 additions & 2 deletions test/SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
<script src="lib/jasmine-2.1.3/boot.js"></script>

<!-- include source files here... -->
<script src="http://peterolson.github.com/BigInteger.js/BigInteger.min.js"></script>
<script src="https://username1565.github.io/BigInteger.js/BigInteger.js"></script>
<script src="../BigRational.js"></script>

<!-- include spec files here... -->
<script src="spec.js"></script>

Expand Down
20 changes: 19 additions & 1 deletion test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ describe("BigRational", function () {
expect(bigRat(1, 4096).toDecimal(5) === "0.00024").toBe(true);
expect(bigRat(-1).toDecimal() === "-1").toBe(true);
expect(bigRat(6.543).toDecimal(0) === "6").toBe(true); // Issue #30

//test percents
expect(bigRat("0.35%").toDecimal() === "0.0035").toBe(true); // Issue #22
expect(bigRat("354.25%").toDecimal(2) === "3.54").toBe(true);
expect(bigRat("2_1/3%").toDecimal(64) === "0.0233333333333333333333333333333333333333333333333333333333333333").toBe(true);
expect(bigRat("1/3%").toDecimal(0) === "0").toBe(true);
expect(bigRat("0.00003%").toDecimal() === "0.0000003").toBe(true);

//test percents
expect(bigRat('0.35%').toString() === "7/2000").toBe(true); // Issue #22
expect(bigRat("354.25%").toString() === "1417/400").toBe(true);
expect(bigRat("2_1/3%").toString() === "7/300").toBe(true);
expect(bigRat("1/3%").toString() === "1/300").toBe(true);
expect(bigRat("0.00003%").toString() === "3/10000000").toBe(true);

//test bigrational from json stringify object //Issue #26
expect(bigRat(JSON.parse(JSON.stringify(bigRat("2_1/3")))).toString() === "7/3").toBe(true);

});
});
describe("toString", function () {
Expand All @@ -157,4 +175,4 @@ describe("BigRational", function () {
expect(+bigRat("1e350").add("1").over("2e350") === 0.5).toBe(true); // Issue #31
});
});
});
});