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!
To submit your source code jump to http://www.codeoftheweek.com/submission.html
If you need to send any additional information you can email it to email@example.com
If you would like to get paid for surfing the web, jump to http://www.codeoftheweek.com/paidsurf.html
In this issue we show how to convert a decimal into a fractional value.
Questions? Email us at firstname.lastname@example.org.
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 email@example.com
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.
See above description.
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
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