Convert Whole.Decimal to Whole-Fraction

hk1

Registered User.
Local time
Today, 04:32
Joined
Sep 1, 2009
Messages
121
I'm wondering if there is an easy way to convert a whole number with a decimal to a whole number followed by a dash and a fraction to represent the decimal? I can program a function that will do this but I was hoping someone knows about a canned solution.

Example: 2.75 becomes 2-3/4
 
I suspect that you will have to go with your home brewed solution. As this seems to be the general response I get from a quick Google on the subject.
 
figures. :)
 
I got one. Just wrote it. Maybe we can compare notes.
 
Well, I haven't written mine yet. :) I wouldn't brag about writing the most elegant or efficient code. What I did start writing consisted of a function to strip off and return the decimal value in a string. From there I just wrote a select case statement to evaluate the most common decimals since I really only need this function to work for: 1/8, 1/4, 1/3, 1/2, 5/8, 3/4, 7/8

Somewhere in the middle of my select case statement I realized I was going to have to have a way to retrieve only the whole number portion of the number and put it together with the fraction string. It's not that that's so hard to do but that's when I started looking around online to see if someone else had a solution.
 
I have a proper working version but the Metric to inch calculations and involve height, width and depth and then a tolerance to ensure that the converted measurements have an overstatement rather than understatement. Clients got annoyed if the object didn't fit were they wanted partcularly if it was only by such a small margin.

First Step my starting point is Metric:
Code:
    SizeHeightValue = .[ArtHeight] / 2.54
    SizeHeightWhole = Int(SizeHeightValue)
    SizeHeightFraction = (SizeHeightValue - SizeHeightWhole) * 100

    If SizeHeightFraction > 75 Then
       SizeResultIns = SizeHeightWhole + 1
    Else
       SizeResultIns = SizeHeightWhole
    End If
            
    If SizeHeightFraction > 1 And SizeHeightFraction <= 25 Then
       SizeResultIns = SizeResultIns & Chr(188)
    ElseIf SizeHeightFraction > 25 And SizeHeightFraction <= 50 Then
       SizeResultIns = SizeResultIns & Chr(189)
    ElseIf SizeHeightFraction > 50 And SizeHeightFraction <= 75 Then
        SizeResultIns = SizeResultIns & Chr(190)
    End If
Remember the caveat there are things in this code (the upward evaluation) that you may wish to ignore.

Simon
 
Here's mine. Also, it only does numbers between 0 and 1. You have to strip off anything to the left of the decimal.
Code:
Function GetFractionFromDecimal(sngDecimal As Single) As String
[COLOR="Green"]'  This function creates a worst-case fraction from the given decimal,
'  passes that fraction to a Greatest Common Divisor calculator, executes
'  the division using the returned GCD, and creates the fraction as a string
[/COLOR]   Dim numerator As Long
   Dim denominator As Long
   Dim i As Long
   
   If sngDecimal <= 0 Or sngDecimal >= 1 Then
[COLOR="Green"]      'only allows decimals between zero and 1[/COLOR]
      GetFractionFromDecimal = "Out of range error."
   Else
[COLOR="Green"]      'create the worst-case fraction: has a power of 10 in the denominator
      'scaled to the length of the decimal[/COLOR]
      denominator = 10 ^ (Len(Trim(sngDecimal)) - 2)
      numerator = sngDecimal * denominator
[COLOR="Green"]      'calculate the Greatest Common Divisor[/COLOR]
      i = GCD(numerator, denominator)
[COLOR="Green"]      'do the divisions and create the string[/COLOR]
      GetFractionFromDecimal = CLng(numerator / i) & "/" & CLng(denominator / i)
   End If
End Function

Private Function GCD(num As Long, den As Long) As Long
[COLOR="Green"]'  This function calculates the Greatest Common Divisor
'  by recursively multiplying together a series of
'  Lowest Common Divisors calculated by LCD(), below[/COLOR]
   Dim i As Long
[COLOR="Green"]   'get an LCD[/COLOR]
   i = LCD(num, den)
   If i > 1 Then
[COLOR="Green"]      'if the LCD > 1 then it might be further reduced, so recur[/COLOR]
      GCD = i * GCD(num / i, den / i)
   Else
[COLOR="Green"]      'recursion anchor[/COLOR]
      GCD = 1
   End If
End Function

Private Function LCD(num As Long, den As Long) As Long
[COLOR="Green"]'  This function calcultes the Lowest Common Divisor
'  by brute force.  A variable i is incremented and tested.
'  If it exceeds the numerator, it fails and a 1 is returned.[/COLOR]
   Dim i As Long
   i = 2
[COLOR="Green"]   'find the first number that divides evenly into the numerator and the denominator[/COLOR]
   Do Until (den Mod i = 0 And num Mod i = 0)
      i = i + 1  [COLOR="Green"] 'keep looking[/COLOR]
      If i > num Then i = 1   [COLOR="Green"]'this will cause the loop to exit[/COLOR]
   Loop
   LCD = i
End Function
 
Mark

I have a question, how can I modify the numerator of your code snip so that instead of it outputting 3/10 for .3 get it to output 1/3. Trying to get fraction like 1/3, 2/3. I'm using it for measure like 1/2, 1/3 cup and all those other thingy's. Other than that it works great. Thanks for your help ahead of time.

Steven :banghead:
 
Last edited:
Well, 0.3 is not the same as 1/3, so to get that result you'd have to break the integrity of the process. Maybe you need to pre-process that number, like round to the nearest 0.25 or something, because 0.3 is almost as close to 1/3 as it is to 1/4.

Code:
1/3 - 0.3 = 0.033333
0.3 - 1/4 = 0.05

0.05 and 0.03333 are very small numbers in a recipe.
 
Thanks for the quick reply. 1/3 = .333333∞ I Just wanted it to get it to read .3 = 1/3 teaspoon or cup in my Nutrition Facts form. I'm working on a nutrition tracking db or as it is now called application in Access 2013. I'm just getting started with Access and VBA, but with the help from Google, forums, and the use of GFA basic with 16 bit Atari computers from years ago I manage to get things to work. Thanks anyway. I'll figure out something.

Steven :banghead:
 
I know this is an old thread, but I must say this sounds like a very difficult exercise. very impressive to get a solultion, I think.
 

Users who are viewing this thread

Back
Top Bottom