Re: Convert decimal to fraction string
- From: "Rick Rothstein \(MVP - VB\)" <rickNOSPAMnews@xxxxxxxxxxxxxxxxx>
- Date: Thu, 1 Mar 2007 19:15:20 -0500
This is probably an idiotic way to get there...
but since I love public humiliation so much <g> here goes my stab at it
....
given a decimal value I want to get a string representation of it's
fractional equivalent
99% of the time I'll be working with "architectural" units eg 1/16, 3/32,
7/64, 3 3/16, etc so my testing below was rather limited
Rick will probably have a one-liner for this or someone will point out a
built in vb function<g>
in any case, observations, corrections appreciated
If it helps you any, here is a response along with a function (actually, two
working in tandem) that I posted a few years back.
Rick
Consider this answer from a previous post of mine (remember, this was an
answer to someone else's question; hence the reference to the number
7.25489)...
Try this function. You can specify the maximum denominator to use in the
optional 2nd parameter. If you leave it out, then a default value of 64 is
used. The function rounds, either up or down, to the *closest* fraction
having the unit of resolution set by the 2nd argument. So, your value of
7.25489 will round to 7-1/4 even though the decimal part is more than 1/4
(it is closer to 1/4 than it is to 17/64, the next fraction past 1/4 using
the 1/64 unit-of-resolution). By the way, the routine does not limit you to
using denominators that are powers of two (although those are probably the
only ones that make sense). That means, if you specify a 2nd parameter of
47, you will receive 7-12/47 as an answer.
If your version of VB only supports optional arguments that are Variants
(and can't be defaulted), then you will have to modify the function
definition accordingly and set the default value of 64 inside of your
function. If your version of VB doesn't support optional arguments at all,
then simply turn it into a required argument.
Function MakeFraction(ByVal DecimalNumber As Variant, _
Optional ByVal LargestDenominator As Long = 64, _
Optional bShowDash As Boolean = False) As String
Dim GCD As Long
Dim TopNumber As Long
Dim Remainder As Long
Dim WholeNumber As Long
Dim Numerator As Long
Dim Denominator As Long
If IsNumber(DecimalNumber) Then
DecimalNumber = CDbl(DecimalNumber)
WholeNumber = Fix(DecimalNumber)
Denominator = LargestDenominator
Numerator = Format(Denominator * _
Abs(DecimalNumber - WholeNumber), "0")
If Numerator = Denominator Then
Numerator = 0
WholeNumber = WholeNumber + 1
End If
If Numerator Then
GCD = LargestDenominator
TopNumber = Numerator
Do
Remainder = (GCD Mod TopNumber)
GCD = TopNumber
TopNumber = Remainder
Loop Until Remainder = 0
Numerator = Numerator \ GCD
Denominator = Denominator \ GCD
MakeFraction = CStr(WholeNumber) & _
IIf(bShowDash, "-", " ") & _
CStr(Numerator) & "/" & _
CStr(Denominator)
Else
MakeFraction = CStr(WholeNumber)
End If
Else
' Input wasn't a number, handle error here
End If
End Function
Function IsNumber(ByVal Value As String) As Boolean
' Leave the next statement out if you don't
' want to provide for plus/minus signs
If Value Like "[+-]*" Then Value = Mid$(Value, 2)
IsNumber = Not Value Like "*[!0-9.]*" And _
Not Value Like "*.*.*" And _
Len(Value) > 0 And Value <> "." And _
Value <> vbNullString
End Function
.
- Follow-Ups:
- References:
- Convert decimal to fraction string
- From: MP
- Convert decimal to fraction string
- Prev by Date: Re: Localized Error Messages
- Next by Date: Re: Function that returns an Array
- Previous by thread: Convert decimal to fraction string
- Next by thread: Re: Convert decimal to fraction string
- Index(es):
Relevant Pages
|
Loading