When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Thursday, May 24, 2001

Adding and Reducing Fractions

By John Peterson and Scott Mitchell

Recently there were a couple of questions on the ASPFreeForAll ListServ regarding fractions. There were a number of fraction-related questions, but two of them:

    - continued -

  1. How does one display a fraction as a decimal (that is, how does one display, say, 17/20 as 0.85),
  2. How does one add two fractions, leaving the result as a fraction (that is, adding 4/5 and 3/2 and getting a result of 23/10), and
  3. How does one reduce a fraction into its lowest terms (that is, if you have the fraction 4/8, you should display it as 1/2)

Let's go about answering those two questions! Before we begin, though, let's take a quick moment to touch up on fraction terminology, just in case it's been too long since you've last used terms like numerator and denominator. :-)

A fraction basically consists of two parts, a numerator and a denominator. The numerator is the top number while the denominator is the bottom one. So, if we have the fraction 2/3, 2 is the numerator while 3 is the denominator. Simple enough, eh? Also, if we have a whole number with a fraction (such as 1 2/5), we can express the whole thing as a fraction by following this simple algorithm: multiple the whole number (1, in this case) by the denominator, adding to that result the numerator. This is the new numerator over the old denominator. So 1 2/5 is equal to 7/5 (1 * 5 + 2 = 7, the new numerator, keep 5 as the denominator).

Displaying a Fraction as a Decimal Value
Displaying a fraction as a decimal value is painfully simple if we have the fraction expressed in terms of two integers, the numerator and denominator. We can simply display the result of the division of the numerator by the denominator:

'Write out the decimal value of our fraction
Response.Write(iNumerator / iDenominator)

Things are a tad more complicated if we have a fraction as a string instead of two numbers. For instance, if we have the fraction 4/5 expressed as a string ("4/5") and we are using VBScript version 5.0 or greater, we can simply use the Eval function to dynamically compute 4/5 from the string "4/5". (To determine what server-side scripting language version you are using, be sure to read: Determining the Server-Side Scripting Language and Version.)

'Write out the decimal value of our fraction
Dim strFraction
strFraction = "4/5" Response.Write(Eval(strFraction))

If you are using a version of VBScript prior to 5.0, you cannot use Eval. Ideally, you should upgrade your scripting engine (a free download from Microsoft), but if you cannot do this, you must use some string handling to pick out the numerator and denominator. The simplest way to do this is to use split. (To learn more about split be sure to read Parsing with join and split!)

'Get the numerator and denominator from a string
Dim strFraction, iNumerator, iDenominator
strFraction = "4/5"

iNumerator = split(strFraction, "/")(0)
iDenominator = split(strFraction, "/")(1)

Once you have the numerator and denominator in decimal form, you can simply perform the division and output the value. If you have your fraction in terms of a whole number along with a fraction (such as 4 3/17), you will need to use the split technique: once with the space (" ") as the delimiter to pick out the whole number and the fraction, and then again on the fraction (using the / as the delimiter) to pick out the numerator and denominator. Then, follow the algorithm given at the start of this article to turn it into a fraction, and then (finally) perform the division.

Adding Two Fractions
Adding two fractions is fairly simple given four integer values: the numerators and denominators of each fraction to add. Once we have those four integer values (call them iNumerator1, iDenominator1, iNumerator2, and iDenominator2), the result of iNumerator1 / iDenominator1 + iNumerator2 / iDenominator2 can be expressed as the fraction:

iNumerator1 * iDenominator2 + iNumerator2 * iDenominator1
          iDenominator1 * iDenominator2

So, to get iAnswerNumerator and iAnswerDenominator (the numerator and denominator of the result of the addition of our two fractions), we simple compute them as follows:

'Add iNumerator1/iDenominator1 to iNumerator2/iDenominator2
iAnswerNumerator = (iNumerator1 * iDenominator2) + (iNumerator2 * iDenominator1)
iAnswerDenominator = (iDenominator1 * iDenominator2)

One caveat when using this approach: your resulting answers will not always be in the reduced form. Imagine that you added 3/4 to 1/2. The above algorithm would produce an answer of 10/8, which, while correct, is not in reduced form. The reduced form of 10/8 is 5/4. This leads nicely into our next section...

Reducing a Fraction
To reduce a fraction, all you need to do is find the largest divisor that the numerator and denominator share. So, if we have the fraction 20/8, the largest divisor 20 and 8 share is 4. Once this divisor is found, we divide both the numerator and denominator parts of the fraction by the divisor, giving us our new, reduced form: (20 / 4) / (8 / 4), which reduces to 5/2. (Note that the fraction and its reduced form are equal: 20/8 and 5/2 are equivalent.)

So the question arises how do we find the largest common divisor? The simplest way is to start a loop counting down from either the numerator or denominator and going to zero. At each iteration of the loop, we want to check if the number is a common divisor between the numerator and denominator. We can quickly check this by seeing if Numerator mod Iteration = 0 and Denominator mod Iteration = 0. If both of these statements are true, then we've found a common divisor (the greatest one, too). (Note that the mod operator calculates the remainder of Numerator / Iteration. Therefore, if the remainder of the division is zero, then Iteration must be a divisor of Numerator.)

This algorithm can be seen in the below snippet of code. Note that we are reducing the fraction we found when adding two fractions together:

' Simplify
For I = iAnswerDenominator To 1 Step -1
  If iAnswerNumerator Mod I = 0 And iAnswerDenominator Mod I = 0 Then
    Exit For
  End If

iAnswerNumerator = iAnswerNumerator / I
iAnswerDenominator = iAnswerDenominator / I

Neat, eh? There are more efficient ways of calculating the greatest common divisor, such as using Euler's Method, but such discussions are beyond the scope of this article.

This article presented three algorithms for working with fractions. We examined how to display fractions as their decimal equivalents, how to add two fractions, getting the result as a fraction, and how to reduce a fraction. Be sure to check out the live demo where we explore all of these algorithms!

Happy Programming!

  • By John Peterson and Scott Mitchell

    Related Links

  • View the live demo!

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article