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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
node_modules
22 changes: 21 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,26 @@ Fraction.prototype.normalize = (function()
* round the number to nine decimal places
* to avoid js floating point funnies
*/

function numberToFixed (x) {
let e
if (Math.abs(x) < 1.0) {
e = parseInt(x.toString().split("e-")[1])
if (e) {
x *= Math.pow(10, e - 1)
x = "0." + (new Array(e)).join("0") + x.toString().substring(2);
}
} else {
e = parseInt(x.toString().split("+")[1])
if (e > 20) {
e -= 20
x /= Math.pow(10, e)
x += (new Array(e + 1)).join("0");
}
}
return x;
}

if (isFloat(this.denominator)) {
var rounded = roundToPlaces(this.denominator, 9);
var scaleup = Math.pow(10, rounded.toString().split('.')[1].length);
Expand All @@ -318,7 +338,7 @@ Fraction.prototype.normalize = (function()
this.numerator *= scaleup;
}
if (isFloat(this.numerator)) {
var rounded = roundToPlaces(this.numerator, 9);
var rounded = numberToFixed(roundToPlaces(this.numerator, 9));
var scaleup = Math.pow(10, rounded.toString().split('.')[1].length);
this.numerator = Math.round(this.numerator * scaleup); // this !!! should be a whole number
//this.numerator *= scaleup;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fractional",
"version": "1.0.0",
"version": "1.0.1",
"description": "fraction arithmetic",
"main": "index.js",
"scripts": {
Expand Down