Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   Index   Templates   Seminars   TechHelp   Forums   Help   Contact   Join   Order   Logon  
 
Home > Courses > Access > Expert > X26 > < X25 | X27 >
Back to Access Expert 26    Comments List
Converting Fractions to number Upload Images   Link   Email  
Garry Frank       
5 months ago
Is there a way to convert a fraction (i.e., Text string such as "1/5") to a number equivalent (0.20) in Access?  This works well in Excel, and if you have a database of recipes which normally would read "1/2" + the measurement, it is not easily read if you use the decimal equivalent, especially on a measurements that do not divide evenly into 1, such as 1/3 (0.3333...).  Yet, when creating a grocery list you need to know how much you need.
Alex Hedley             
5 months ago
Can't remember if it's covered in Math in Fields.
Adam Schwanz             
5 months ago
If not, you can use InStr to find the "/" then use something like Left to split the two sets of numbers and divide them.
Kevin Robertson            
5 months ago
Are these the results you are looking for (see screenshot)?
Kevin Robertson            
5 months ago

Kevin Robertson            
5 months ago
I updated the function to take into consideration values like '1 1/2', '5 2/4' and so on.
Also, Null values are also covered and will return a zero (0).

Public Function FracToDec(Fraction As Variant) As Double

    Dim FracArray() As String, SpacePos As Long, WholeNum As Long, MyFrac As String
    
    On Error GoTo ErrorHandler
    
    WholeNum = 0
    SpacePos = Nz(InStr(Fraction, " "), 0)
    
    If SpacePos > 0 Then
        WholeNum = Trim(Left(Fraction, SpacePos))
        MyFrac = Trim(Right(Fraction, Len(Fraction) - SpacePos))
    Else
        MyFrac = Nz(Fraction, 0)
    End If
    
    FracArray = Split(MyFrac, "/")

    If Not IsNull(Fraction) Then
        FracToDec = WholeNum + (Round(Val(FracArray(0)) / Val(FracArray(1)), 2))
    End If
    
    Exit Function
    
ErrorHandler:
    FracToDec = 0
    
End Function


The screenshot below shows the results.
Kevin Robertson            
5 months ago

Garry Frank       
5 months ago
Wow!  Thanks.  I appreciate the input.  Yes, the code is very helpful.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Expert 26.
 

 
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2024 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/27/2024 2:42:56 AM. PLT: 2s