Visual Basic Code of the Week (COTW)
http://www.codeoftheweek.com
Issue #99
Online Version at http://www.codeoftheweek.com/membersonly/bi/0099.html (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp. Word, Excel and Visual Basic are trademarks of Microsoft Corp.

## Our next issue is number 100!! Get 100 bucks now!

Can you believe we have published 99 issues of Code of the Week already?! We sure can't. To celebrate our 100th issue we will be offering \$100 to the best source code submission from our readers. Please make sure you do not duplicate any existing issues. All submissions must be received by September 4, 1999. By the way, even if you do not submit the winning source code you might be published in the future. We pay \$25 for complete submissions which get published. So send in that source code!

If you need to send any additional information you can email it to issue100@codeoftheweek.com

## Get paid to surf the web!

If you would like to get paid for surfing the web, jump to http://www.codeoftheweek.com/paidsurf.html

## Requirements

• Visual Basic 4.0 32-bit or higher.

## In this Issue

In this issue we show how to convert a decimal into a fractional value.

Questions? Email us at questions@codeoftheweek.com.

#### basMath

This issue is an extension of issue number #19 (Converting fractional values to decimal values). This issue uses the Greatest Common Divisor algorithm to convert any decimal up to 9 places into a fraction.

If you have any questions about using this routine, let us know at questions@codeoftheweek.com

#### Functions

```Public Function GCD(ByVal a As Long, ByVal b As Long) As Long
```

Find the Greatest Common Divisor using Euclid's algorithm.

```Public Function DecimalToFraction(decDecimal As Variant) As String
```

Using the above GCD function determine the fractional value of the decimal value passed in by decDecimal. This number can include a whole part and a fractional part, such as 45.4311. For best results you should pass in a Decimal data type by using the CDec function. This will preserve the decimal places in the most accurate manner.

If more than 9 decimal places are passed an error will be raised to the caller. It will return a string which contains the mixed number fraction (if a whole number was specified). The format of the return value will be xx yy/zz where xx is the whole number portion, yy is the numerator and zz is the denominator. If xx is zero then it will not be shown.

#### Returns

See above description.

#### Sample Usage

This sample will show a few calls to the DecimalToFraction function.

```  Debug.Print DecimalToFraction(322.875)
322 7/8

Debug.Print DecimalToFraction(-0.459)
-459/1000

Debug.Print DecimalToFraction(.0125)
1/80

Debug.Print DecimalToFraction(-341.0125)
-341 1/80
```

## Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address: http://www.codeoftheweek.com/membersonly/bi/0099.html

## This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.