fractionfraction.js

A javascript library for math Fractions

Source

Setup

<script src="fraction.js"></script>

Create a Fraction

//These all create same fraction 1/2
var frac = new Fraction(1,2);
var frac = new Fraction(0.5);
var frac = new Fraction("0.5");
var frac = new Fraction("1/2");
var frac = new Fraction("1/2");
var frac = new Fraction("1", "2");

var frac2 = new Fraction(frac);//from other fraction

Add Fractions

var frac1 = new Fraction(1,2);
var frac2 = new Fraction(2,3);
var frac3 = new Fraction(3,4);

//You can add many fractions at once
frac1.add(frac2,frac3);
frac1.add(frac2,frac3,"4/5",0.2);//Even numbers and Fraction in string can be added
frac1.add([frac2,frac3,"4/5",0.2]);//You can pass array too

//Another ways for adding
Fraction.add(frac2,frac3);
Fraction.add(frac2,frac3,"4/5",0.2);
Fraction.add([frac2,frac3,"4/5",0.2]);

Subtract Two Fractions

frac1.subtract(frac2);
//Even numbers and Fraction in string can be subtracted
frac1.subtract("4/5");
frac1.subtract(0.2);

//Another ways for subtracting
Fraction.subtract(frac1,frac2);
Fraction.subtract(frac1,"4/5");
Fraction.subtract(frac1,0.2);

Multiply Fractions

//You can multiply many fractions at once
frac1.multiply(frac2,frac3);
frac1.multiply(frac2,frac3,"4/5",0.2);//Even numbers and Fraction in string can be multiplied
frac1.multiply([frac2,frac3,"4/5",0.2]);//You can pass array too

//Another ways for multiplying
Fraction.multiply(frac2,frac3);
Fraction.multiply(frac2,frac3,"4/5",0.2);
Fraction.multiply([frac2,frac3,"4/5",0.2]);

Divide Two Fractions

frac1.divide(frac2);
//Even numbers and Fraction in string can be divided
frac1.divide("4/5");
frac1.divide(0.2);

//Another ways for dividing
Fraction.divide(frac1,frac2);
Fraction.divide(frac1,"4/5");
Fraction.divide(frac1,0.2);

//You can drop "divide" too :)
Fraction(frac1,frac2);
Fraction(frac1,"4/5");
Fraction(frac1,0.2);
Fraction("1/3",0.2);

Get Reciprocal

frac1.reciprocal();//Converts frac1 to (1/frac)
Fraction.reciprocal(frac1);

Add Reciprocals

//You can pass many fractions at once
Fraction.addReciprocals(frac1,frac2,frac3);//=(1/frac1)+(1/frac2)+(1/frac3)
Fraction.addReciprocals(frac1,"1/6",0.3);//Even numbers and Fraction in string can be passed
Fraction.addReciprocals([frac1,"1/6",0.3]);//You can pass array too

Compare Fractions

//is frac1=frac2?
frac1.equals(frac2);
Fraction.equals(frac1,frac2);
//do not use frac1==frac2

//general comparisons
frac1>frac2;
frac1<frac2;
frac1>=frac2;
frac1<=frac2;

Compare Fraction to a number

//is frac1=number?
frac1==0.3;
frac1.equals(0.3);
Fraction.equals(frac1,0.3);

//general comparisons
frac1>0.3;
frac1<0.3;
frac1>=0.3;
frac1<=0.3;

Get Remainder

//Get r, as x/y = q + (r/y), where q & r are integers
frac1.remainder();
Fraction.remainder(frac1);

Get Quotient

//Get q, as x/y = q + (r/y), where q & r are integers
frac1.quotient();
Fraction.quotient(frac1);

Get in Decimal

frac1.valueOf();
Fraction.valueOf(frac1);