Results 1 to 11 of 11

Thread: [RESOLVED] decimals to fractions

  1. #1

    Thread Starter
    Hyperactive Member VB4fun's Avatar
    Join Date
    May 2003
    Location
    too far from Fiji
    Posts
    342

    Resolved [RESOLVED] decimals to fractions

    Known: Excel stores values as decimals, even when it displays decimals.

    Know: When you bring the value to a label on the form, it displays the decimal.

    Unknown: Is there an easy way to make it show the same fraction displayed in excel?
    Talk does not cook rice.
    -Chinese Proverb

  2. #2
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: decimals to fractions

    What is the range of formats in a particular cell on your Excel sheet? If you can force the format to a fixed format, you can use the same format for the text in your Label.

    There is an Excel function to determine the format for any cell (see CELL function).

    I think what you are trying to do is to take the identical text string representation of a decimal number that appears in the Excel sheet cell and put that same string in your label. Is this a correct interpretation?
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  3. #3

    Thread Starter
    Hyperactive Member VB4fun's Avatar
    Join Date
    May 2003
    Location
    too far from Fiji
    Posts
    342

    Re: decimals to fractions

    I am not sure how the Excel function would help me with this issue, unless I am missing something.

    think what you are trying to do is to take the identical text string representation of a decimal number that appears in the Excel sheet cell and put that same string in your label. Is this a correct interpretation?
    Correct, a Label on a form.

    Excel Cell A15 = 1/4"

    Form Label lblFraction.caption = .25

    Desired result lblFraction.caption = 1/4"
    Talk does not cook rice.
    -Chinese Proverb

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: decimals to fractions

    Is the value in cell A15 always going to be a rational number? i.e. one that can be represented as a fraction.
    If the value in the cell is irrational, like Pi or e, what would you like to display on the label?
    Can the value in A15 evcer be greater than 1?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

    Thread Starter
    Hyperactive Member VB4fun's Avatar
    Join Date
    May 2003
    Location
    too far from Fiji
    Posts
    342

    Re: decimals to fractions

    Actually, value of the cell is always going to be a measurement/dimension:
    23 1/4, 34 5/8, 19 15/16.

    There are times it may be whole:
    27, 56, 82

    What I would like displayed on the form, is what you see here, I can take care of what happens in Excel with functions, etc. for my calculations,etc.
    Talk does not cook rice.
    -Chinese Proverb

  6. #6
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: decimals to fractions

    On your Excel sheet ... in a typical cell ... do you see 23 1/4 or do you see 23.25? If youi see "23 1/4" it must be a text string, and you should be able to just copy the text into the Label.

    Is 1/16" the finest resolution you are dealing with, or are you using smaller fractions? What is the smallest fraction?
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  7. #7

    Thread Starter
    Hyperactive Member VB4fun's Avatar
    Join Date
    May 2003
    Location
    too far from Fiji
    Posts
    342

    Re: decimals to fractions

    I see 23 1/4, because the format is set to that. Just because I see 23 1/4 does not mean it is a text string.

    The smallest dimension is probably going to be 1"
    The smallest fractional portion of the number is probably going to 1/32" (i.e. 24 1/32")

    But not sure why this would have a bearing..?
    Talk does not cook rice.
    -Chinese Proverb

  8. #8
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: decimals to fractions

    The attached function will give you a textual representation of a fractional value.
    For irrational value it will only give an approximate fraction. The accuracy of that approximation can be increased by increasing the value of the Percision variable.

    VB Code:
    1. Function FractDisplay(MyDecimal As Double, Optional Precision As Integer = 100) As String
    2. Dim Neg As Boolean
    3. Dim Whole As Integer
    4. Dim WholePart As String
    5. Dim Numer As Integer
    6. Dim Denom As Integer
    7. Dim Frac As Double
    8. Dim ChosenFrac As Double
    9.    
    10.     Neg = MyDecimal < 0
    11.    
    12.     MyDecimal = Abs(MyDecimal)
    13.     Whole = Int(MyDecimal)
    14.    
    15.     Select Case CDbl(Whole)
    16.         Case MyDecimal
    17.             FractDisplay = CStr(Whole)
    18.             GoTo NegCheck
    19.         Case 0
    20.             WholePart = ""
    21.         Case Else
    22.             WholePart = CStr(Whole) & " "
    23.     End Select
    24.    
    25.     MyDecimal = MyDecimal - Whole
    26.    
    27.     ChosenFrac = 0
    28.     For Denom = 2 To Precision
    29.         For Numer = 1 To (Precision - 1)
    30.             Frac = Numer / Denom
    31.             If Frac = MyDecimal Then
    32.                 FractDisplay = WholePart & CStr(Numer) & "/" & CStr(Denom)
    33.                 GoTo NegCheck
    34.             End If
    35.             If Abs(Frac - MyDecimal) < Abs(ChosenFrac - MyDecimal) Then
    36.                 FractDisplay = WholePart & CStr(Numer) & "/" & CStr(Denom)
    37.                 ChosenFrac = Frac
    38.             End If
    39.         Next Numer
    40.     Next Denom
    41.  
    42. NegCheck:
    43.     If Neg Then FractDisplay = "-" & FractDisplay
    44. End Function
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  9. #9
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: decimals to fractions

    Sorry guys, one error in the above. The Numerator only need to loop through to the value of the Denominator, not the precision. Code below has been ammended.

    VB Code:
    1. Function FractDisplay(MyDecimal As Double, Optional Precision As Integer = 100) As String
    2. Dim Neg As Boolean
    3. Dim Whole As Integer
    4. Dim WholePart As String
    5. Dim Numer As Integer
    6. Dim Denom As Integer
    7. Dim Frac As Double
    8. Dim ChosenFrac As Double
    9.    
    10.     Neg = MyDecimal < 0
    11.    
    12.     MyDecimal = Abs(MyDecimal)
    13.     Whole = Int(MyDecimal)
    14.    
    15.     Select Case CDbl(Whole)
    16.         Case MyDecimal
    17.             FractDisplay = CStr(Whole)
    18.             GoTo NegCheck
    19.         Case 0
    20.             WholePart = ""
    21.         Case Else
    22.             WholePart = CStr(Whole) & " "
    23.     End Select
    24.    
    25.     MyDecimal = MyDecimal - Whole
    26.    
    27.     ChosenFrac = 0
    28.     For Denom = 2 To Precision
    29.         For Numer = 1 To (Denom - 1)
    30.             Frac = Numer / Denom
    31.             If Frac = MyDecimal Then
    32.                 FractDisplay = WholePart & CStr(Numer) & "/" & CStr(Denom)
    33.                 GoTo NegCheck
    34.             End If
    35.             If Abs(Frac - MyDecimal) < Abs(ChosenFrac - MyDecimal) Then
    36.                 FractDisplay = WholePart & CStr(Numer) & "/" & CStr(Denom)
    37.                 ChosenFrac = Frac
    38.             End If
    39.         Next Numer
    40.     Next Denom
    41.  
    42. NegCheck:
    43.     If Neg Then FractDisplay = "-" & FractDisplay
    44. End Function
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  10. #10
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: decimals to fractions

    I didn't even know that Excel had fractional notation! I've never needed it though. Learn something new every day!
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  11. #11

    Thread Starter
    Hyperactive Member VB4fun's Avatar
    Join Date
    May 2003
    Location
    too far from Fiji
    Posts
    342

    Re: decimals to fractions

    DKenny you rock !

    Works perfectly, Thanks!
    Talk does not cook rice.
    -Chinese Proverb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width