public class Fraction {
private constructor ( public numerator: Integer, public denominator: Integer);
public constructor valueOf ( value: Decimal){
return new Fraction.parse(value.toString());
}
public constructor parse (value : String){
let pos : Natural? = value.indexOf('.');
if (pos != none){
let wholePart = new Integer.parse(value.subString(0,pos));
let multiplier = 10 ^^ (value.size - pos); // 10 to the power of the number of decimal digits
let decimalPart = new Integer.parse(value.subString(pos+1));
let numerator = wholePart * multiplier + decimalPart;
return new Fraction(numerator, multiplier);
} else {
// whole number
return new Fraction(new Integer.parse(value), 1);
}
}
public multiply ( other : Fraction) : Fraction{
return new Fraction ( this.numerator * other.numerator, this.denominator * other.denominator);
}
public invert () : Fraction{
return new Fraction (this.denominator, this.numerator);
}
}