Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,635 questions

55,370 answers

573 users

How to convert a decimal number to a rational number in VB.NET

1 Answer

0 votes
Imports System
Imports System.Numerics

'
' DecimalToRational
' -----------------
' Converts a decimal number (given as a string) into a rational number p/q.
'
' Why parse the string?
'   • VB.NET has no built‑in rational type.
'   • Double/Single cannot preserve exact decimal digits.
'   • Using strings + BigInteger ensures perfect accuracy.
'
' Algorithm:
'   1. Look for a decimal point.
'   2. If none → integer → numerator = n, denominator = 1.
'   3. Otherwise:
'         Example: "12.345"
'         integer part   = 12
'         fractional part = 345
'         digits = 3
'
'         numerator   = integer_part * 10^digits + fractional_part
'         denominator = 10^digits
'
'   4. Reduce using gcd.
'

Module DecimalToRational

    ' Simple container for numerator/denominator
    Structure Rational
        Public Numerator As BigInteger
        Public Denominator As BigInteger

        Public Overrides Function ToString() As String
            Return Numerator.ToString() & "/" & Denominator.ToString()
        End Function
    End Structure

    ' Convert decimal string to Rational
    Function ConvertDecimalToRational(s As String) As Rational
        Dim dotPos As Integer = s.IndexOf("."c)

        If dotPos = -1 Then
            ' No decimal point → integer
            Dim num As New BigInteger(Long.Parse(s))
            Return New Rational With {.Numerator = num, .Denominator = BigInteger.One}
        End If

        ' Split into integer and fractional parts
        Dim intPart As String = s.Substring(0, dotPos)
        Dim fracPart As String = s.Substring(dotPos + 1)

        Dim integerValue As New BigInteger(Long.Parse(intPart))
        Dim fractionalValue As New BigInteger(Long.Parse(fracPart))

        Dim digits As Integer = fracPart.Length

        ' Build denominator = 10^digits
        Dim denominator As BigInteger = BigInteger.Pow(10, digits)

        ' Build numerator
        Dim numerator As BigInteger = integerValue * denominator + fractionalValue

        ' Reduce using gcd
        Dim g As BigInteger = BigInteger.GreatestCommonDivisor(numerator, denominator)
        numerator = numerator / g
        denominator = denominator / g

        Return New Rational With {.Numerator = numerator, .Denominator = denominator}
    End Function

    Sub Main()
        Dim values() As String = {
            "3.5", "12.75", "0.125", "100.001",
            "7", "42.0", "0.333", "5.2"
        }

        For Each v In values
            Dim r As Rational = ConvertDecimalToRational(v)
            Console.WriteLine(v & " -> " & r.ToString())
        Next
    End Sub

End Module


'  run:
'
'  3.5 -> 7/2
'  12.75 -> 51/4
'  0.125 -> 1/8
'  100.001 -> 100001/1000
'  7 -> 7/1
'  42.0 -> 42/1
'  0.333 -> 333/1000
'  5.2 -> 26/5
'

 



answered Jul 23 by avibootz
...