mirror of
https://github.com/Brandon-Rozek/Fractions.js.git
synced 2024-11-09 11:10:34 -05:00
Fixed bug with single argument negative values
This commit is contained in:
parent
a0849389a7
commit
62b850380d
2 changed files with 7 additions and 1 deletions
|
@ -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)); }
|
||||||
}
|
}
|
||||||
|
|
1
Tests.js
1
Tests.js
|
@ -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));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue