Fraction Class
Description
The Fraction class allows you to store and perform basic mathematical functions on fractions.
Example
fraction1:
fraction2:
fraction1.ToString() fraction2.ToString()
fraction1.ToImproperString() fraction2.ToImproperString()
fraction1.Absolute() fraction2.Absolute()
-fraction1 -fraction2
 
fraction1 + fraction2 = fraction1 < fraction2 =
fraction1 - fraction2 = fraction2 < fraction1 =
fraction2 - fraction1 = fraction1 > fraction2 =
fraction1 * fraction2 = fraction2 > fraction1 =
fraction1 / fraction2 = fraction1 = fraction2 =
fraction2 / fraction1 = fraction1 <> fraction2 =
fraction1 <= fraction2 =
fraction2 <= fraction1 =
fraction1 >= fraction2 =
fraction2 >= fraction1 =
Properties & Methods
FractionFraction is a class that allows you to store and perform basic mathematical functions on fractions.
  • Sign - An integer specifying whether the fraction is positive or negative
  • WholePart - The whole part of the fraction
  • Numerator - The numerator of the fractional part of the fraction
  • Denominator - The denominator of the fraction. If you attempt to assign a value of 0 to this property an exception will be thrown
  • FractionRegularExpression - A string constant containing a regular expression that can be used to validate whether a string is a valid fraction
Fraction()Constructor that creates a fraction with a value of 0 Fraction(frac As String)Constructor that accepts a fraction as a string
  • frac - A String that is of any of the following formats: [-]a b/c, [-]a, or [-]a/b
Fraction(sign As Integer,intpart As ULong,num As ULong,denom As ULong) Fraction(intpart As ULong,num As ULong,denom As ULong) Fraction(num As ULong,denom As ULong)Constructors that create a new Fraction using a sign and integral values
  • sign - An integer specifying whether the fraction is positive or negative. Even though any integer will be accepted, only the sign will be stored
  • intpart - The whole part of the fraction
  • num - The numerator of the fractional part of the fraction. If this is larger than the denominator, it will be adjusted along with the whole part
  • denom - The denominator of the fraction
Fraction(value As Decimal) Fraction(value As Double)Constructor that creates a Fraction from a value of type Decimal or Double
  • value - A value of type Decimal or Double. Please keep in mind that the value used is the value returned by the Decimal or Double's ToString() method, and that the exact value is used (for example, .33333 is not equal to 1/3)
ToString()Displays the fraction in a standard fraction format of [-]a b/c, [-]b/c, or [-]a, where the fractional component, if present, is always less than 1 ToImproperString()Displays the fraction as either an improper fraction or an integer if there is no fractional component Absolute()Returns the absolute value Defined Operators:
  • + - Addition
  • - - Subtraction
  • * - Multiplication
  • / - Division
  • - - Negation
  • < - Less Than
  • > - Greater Than
  • = - Equality
  • <> - Inequality
  • <= - Less Than Or Equal To
  • >= - Greater Than Or Equal To
Defined Type Conversions (narrowing):
  • Fraction to SByte
  • Fraction to Byte
  • Fraction to Short
  • Fraction to UShort
  • Fraction to Integer
  • Fraction to UInteger
  • Fraction to Long
  • Fraction to ULong
  • Fraction to Decimal
  • Fraction to Single
  • Fraction to Double
  • Fraction to LargeDecimal
  • LargeDecimal to Fraction
  • LDFraction to Fraction
  • Decimal to Fraction
  • Double to Fraction
Defined Type Conversions (widening):
  • Long to Fraction
  • ULong to Fraction
  • Fraction to LDFraction
Source Code
Fraction.vb:

Remarks
Even though I have defined the type conversions for them, I recommend using caution when converting to any of the standard numeric types since not all possible values of the Fraction class can be stored in them.