LDFraction Class
Description
The LDFraction class is a variation of the Fraction class that uses the LargeDecimal type rather than ULong to store the values.
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
LDFractionLDFraction 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
LDFraction()Constructor that creates a fraction with a value of 0 LDFraction(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
LDFraction(sign As Integer,intpart As LargeDecimal,num As LargeDecimal,denom As LargeDecimal) LDFraction(intpart As LargeDecimal,num As LargeDecimal,denom As LargeDecimal) LDFraction(num As LargeDecimal,denom As LargeDecimal)Constructors that create a new LDFraction 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. The overloads that do not use this parameter assume the input is positive and use the absolute value of the other parameters
  • 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
LDFraction(value As Decimal) LDFraction(value As Double)Constructor that creates an LDFraction 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):
  • LDFraction to SByte
  • LDFraction to Byte
  • LDFraction to Short
  • LDFraction to UShort
  • LDFraction to Integer
  • LDFraction to UInteger
  • LDFraction to Long
  • LDFraction to ULong
  • LDFraction to Decimal
  • LDFraction to Single
  • LDFraction to Double
  • LDFraction to LargeDecimal
  • LDFraction to Fraction
Defined Type Conversions (widening):
  • Long to LDFraction
  • ULong to LDFraction
  • Decimal to LDFraction
  • Double to LDFraction
  • Fraction to LDFraction
  • LargeDecimal to Fraction
Source Code
LDFraction.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 LDFraction class can be stored in them.