Solved

VB.net help--How to

Posted on 2013-02-04
3
135 Views
Last Modified: 2015-01-05
I am fairly new to VB.net and need to  know how to convert/display 16th's and 32nd's from stored db numbers.    Numbers in the are stored like below.

32.8125000    This is a 16th
27.5937500    this is a 32nd.  

I need 32.812500 to print as 32-13/16    and  27.5937500  as 27-19/32.  

Does vb.net have any internal functions to convert/display these type of numbers.
Thanks
0
Comment
Question by:tambrosi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 38851687
how would the program KNOW to display something as said fraction

IE
Ifi i had 5 numbers

10
9
8
7
6

how would I know that  10 should be in 32
9 should be in 16
8 should be in 32
7 should be in 16
6 should be in 32 ????
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 38851812
There is no built in function:

You could use a pre-built function written by others:
http://www.codeproject.com/Articles/165320/Easy-Way-of-Converting-a-Decimal-to-a-Fraction
(using http://www.developerfusion.com/tools/convert/csharp-to-vb/ to convert)

Or:

You could just build a lookup table:
Dim Dec2Frac as new Dictionary(string, string)

Dec2Frac.Add(".8125", " 1/16") 'note the space in the fraction
Dec2Frac.Add(".59375", " 1/32")
Dim theNumber As String 
Dim myVal As Double = 27.59375 
theNumber = myVal.ToString() 
If theNumber.Contains("."C) Then 
      theNumber = theNumber.Replace(theNumber.ToString().Split("."C)(1), Dec2Frac(theNumber.ToString().Split("."C)(1).Trim("0"C))) 
   Else 
      theNumber = theNumber.Trim("0"C).Trim("."C) 
End If

Open in new window

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 38851882
Try something like this?
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        TextBox2.Text = DecToImperialFraction(TextBox1.Text)
    End Sub

    Private Function DecToImperialFraction(ByVal strDecimal As String) As String
        Const ThirtySecond As Decimal = 0.03125
        Dim ret As String = ""
        Dim dec As Decimal
        If Decimal.TryParse(strDecimal, dec) Then
            Dim int As Integer = CInt(Math.Truncate(dec))
            ret = int
            dec = dec - int
            If dec > 0 Then
                Dim numerator As Integer = CInt(dec / ThirtySecond)
                If numerator > 0 Then
                    Dim denomiator As Integer = 32
                    While (numerator Mod 2 = 0 AndAlso denomiator Mod 2 = 0) OrElse (numerator Mod 3 = 0 AndAlso denomiator Mod 3 = 0)
                        If numerator Mod 2 = 0 AndAlso denomiator Mod 2 = 0 Then
                            numerator = numerator / 2
                            denomiator = denomiator / 2
                        Else
                            numerator = numerator / 3
                            denomiator = denomiator / 3
                        End If
                    End While
                    ret = ret & "-" & numerator & "/" & denomiator
                End If
            End If
        End If
        Return ret
    End Function

End Class

Open in new window

0

Featured Post

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question