|
-
Dec 8th, 2005, 03:07 PM
#1
Thread Starter
Hyperactive Member
[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
-
Dec 9th, 2005, 09:47 AM
#2
Frenzied Member
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
-
Dec 9th, 2005, 10:35 AM
#3
Thread Starter
Hyperactive Member
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
-
Dec 9th, 2005, 11:06 AM
#4
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 
-
Dec 9th, 2005, 11:13 AM
#5
Thread Starter
Hyperactive Member
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
-
Dec 9th, 2005, 11:24 AM
#6
Frenzied Member
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
-
Dec 9th, 2005, 11:50 AM
#7
Thread Starter
Hyperactive Member
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
-
Dec 9th, 2005, 11:51 AM
#8
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:
Function FractDisplay(MyDecimal As Double, Optional Precision As Integer = 100) As String
Dim Neg As Boolean
Dim Whole As Integer
Dim WholePart As String
Dim Numer As Integer
Dim Denom As Integer
Dim Frac As Double
Dim ChosenFrac As Double
Neg = MyDecimal < 0
MyDecimal = Abs(MyDecimal)
Whole = Int(MyDecimal)
Select Case CDbl(Whole)
Case MyDecimal
FractDisplay = CStr(Whole)
GoTo NegCheck
Case 0
WholePart = ""
Case Else
WholePart = CStr(Whole) & " "
End Select
MyDecimal = MyDecimal - Whole
ChosenFrac = 0
For Denom = 2 To Precision
For Numer = 1 To (Precision - 1)
Frac = Numer / Denom
If Frac = MyDecimal Then
FractDisplay = WholePart & CStr(Numer) & "/" & CStr(Denom)
GoTo NegCheck
End If
If Abs(Frac - MyDecimal) < Abs(ChosenFrac - MyDecimal) Then
FractDisplay = WholePart & CStr(Numer) & "/" & CStr(Denom)
ChosenFrac = Frac
End If
Next Numer
Next Denom
NegCheck:
If Neg Then FractDisplay = "-" & FractDisplay
End Function
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Dec 9th, 2005, 12:06 PM
#9
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:
Function FractDisplay(MyDecimal As Double, Optional Precision As Integer = 100) As String
Dim Neg As Boolean
Dim Whole As Integer
Dim WholePart As String
Dim Numer As Integer
Dim Denom As Integer
Dim Frac As Double
Dim ChosenFrac As Double
Neg = MyDecimal < 0
MyDecimal = Abs(MyDecimal)
Whole = Int(MyDecimal)
Select Case CDbl(Whole)
Case MyDecimal
FractDisplay = CStr(Whole)
GoTo NegCheck
Case 0
WholePart = ""
Case Else
WholePart = CStr(Whole) & " "
End Select
MyDecimal = MyDecimal - Whole
ChosenFrac = 0
For Denom = 2 To Precision
For Numer = 1 To (Denom - 1)
Frac = Numer / Denom
If Frac = MyDecimal Then
FractDisplay = WholePart & CStr(Numer) & "/" & CStr(Denom)
GoTo NegCheck
End If
If Abs(Frac - MyDecimal) < Abs(ChosenFrac - MyDecimal) Then
FractDisplay = WholePart & CStr(Numer) & "/" & CStr(Denom)
ChosenFrac = Frac
End If
Next Numer
Next Denom
NegCheck:
If Neg Then FractDisplay = "-" & FractDisplay
End Function
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Dec 9th, 2005, 01:04 PM
#10
Frenzied Member
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
-
Dec 9th, 2005, 02:11 PM
#11
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|