Fractions

  • Thread starter Thread starter Nathan
  • Start date Start date
N

Nathan

Is there a simple method of adding, subtracting, multiplying and dividing
fractions in VB.NET, such as 4/9 - 1/3 = 1/9?
 
Nathan,
If you want to preserve the numerator & denominator you will need to create
you own class or structure to do this. (I would recommend a Structure as
structures behave more like value types). Other wise I would recommend the
Decimal, Single, and Double data types (which may have subtle rounding
issues).

Matthew MacDonald's book "Microsoft Visual Basic .NET Programmer's Cookbook"
from MS Press has a topic that covers creating a simple Fraction Class. It
has Add, subtract, Multiply & Divide methods. If I were to implement
Matthew's sample, I would use a Structure and I would make it immutable. So
it behaved more like other primitive types.

FWIW: We need to wait for Whidbey (VS.NET 2004) to allow the above Fraction
type to be extended to allow overloaded operators. Overloaded operators
allows you to use the + operator instead of the Add method.

Hope this helps
Jay
 
Back
Top