Fixed bug with single argument negative values

This commit is contained in:
Brandon Rozek 2015-12-22 00:42:56 -05:00
parent a0849389a7
commit 62b850380d
2 changed files with 7 additions and 1 deletions

View file

@ -193,9 +193,14 @@ Fraction.decimalToFraction = function(x) {
var decLocation = x.indexOf('.'); var decLocation = x.indexOf('.');
if (decLocation != -1) { if (decLocation != -1) {
var whole = x.substring(0, decLocation); var whole = x.substring(0, decLocation);
var isNegative = (whole.indexOf('-') != -1)? true: false;
var remainder = x.substring(decLocation + 1, x.length); var remainder = x.substring(decLocation + 1, x.length);
var nthPlace = Math.pow(10, remainder.length); var nthPlace = Math.pow(10, remainder.length);
return Fraction.add(new Fraction(Number(whole), 1), new Fraction(Number(remainder), nthPlace)) if (isNegative) {
return Fraction.subtract(new Fraction(Number(whole), 1), new Fraction(Number(remainder), nthPlace))
} else {
return Fraction.add(new Fraction(Number(whole), 1), new Fraction(Number(remainder), nthPlace))
}
} }
else { return new Fraction(Number(x)); } else { return new Fraction(Number(x)); }
} }

View file

@ -116,6 +116,7 @@ section("Helper Functions -- Advanced", function() {
assert(Fraction.decimalToFraction(.25), new Fraction(1,4)); assert(Fraction.decimalToFraction(.25), new Fraction(1,4));
assert(Fraction.decimalToFraction(1.7), new Fraction(17, 10)); assert(Fraction.decimalToFraction(1.7), new Fraction(17, 10));
assert(Fraction.decimalToFraction(8), new Fraction(8,1)); assert(Fraction.decimalToFraction(8), new Fraction(8,1));
assert(Fraction.decimalToFraction(-.5), new Fraction(-1,2));
}); });
}); });