Convert decimal numbers to fractions

New product

With this programming algorithm you can convert decimal numbers to fractions. Developed in VB.NET.

Convert decimal numbers to fractions

Today in the blog to complete a bit with the work of rational numbers, I bring you a function that does the opposite work. Convert a number with decimals to fraction in an interesting way, so, without further ado, I show you.

DarListaFracciones delivers a list of approximate fractions to the number with decimals given by the argument d.

As the program as part of the structure of NumerosRacionales, as you can see in the section:

 ListFractions.Add (NewNumeroRacional (numerators (i), denominators (i)))

Crea un numero racional. Si quieren usarla en sus proyectos, solo deben sustituir por bien sea una cadena de texto o su propia estructura de la siguiente forma:

 ListaFracciones.Add(numeradores(i) & “/” & denominadores(i)))

Create a rational number If you want to use it in your projects, you should only replace it by either a text string or your own structure in the following way:

    Shared Function DarListaFracciones(d As Double) As List(Of NumeroRacional)

Dim numerators() As Integer = {0, 1}
Dim denominators() As Integer = {1, 0}
Dim ListFractions As New List(Of NumberRational)

Dim maxNumber As Long = GiveMaximumNumber(d)
Dim d2 As Double = d

Dim calcD As Double = Double.NaN
Dim prevCalcD As Double = Double.NaN

'"for" to avoid blocking by infinite loop
For i = 2 To 999
ReDim Preserve numerators(numerators.Length + 1)
ReDim Preserve denominators(denominators.Length + 1)

Dim L2 As Double = Math.Floor(d2)
numerators(i) = L2 * numerators(i - 1) + numerators(i - 2)
If (Math.Abs(numerators(i)) > maxnumber) Then Exit For

denominators(i) = L2 * denominators(i - 1) + denominatorses(i - 2)
calcD = numerators(i) / denominators(i)
If (calcD = prevCalcD) Then Exit For

ListaFracciones.Add(New RutionalNumber(numerators(i), denominadores(i)))

If (calcD = d) Then Exit For

prevCalcD = calcD
d2 = 1 / (d2 - L2)
End Function

With this function, what is done is to eliminate the comma of the number and gather everything as a block so that it is the maximum possible numerator to do the iterative division.

Shared Function DarMaximoNumber(f As Double) As Long
digitos As Long = 0
Dim DecimalSeparator As String = My.Application.Culture.NumberFormat.NumberDecimalSeparator

Dim ix As Integer = f.ToString().IndexOf(DecimalSeparator)
If ix <> -1 Then digitos = CLng(f.ToString().Remove(ix, 1))

Return digits
End Function

It's all for now, I'll soon get the application out so they can see the programs working.

Follow us on our social networks

 NOTE: Do not forget to follow us on our facebook and twitter social networks so that you become part of our community and receive the notification every time we post something new, so you will not miss anything.


                                                                                                Written by:


Elimar García

Eli Program


Lo que estaba esperando

Esta funcion es la que estaba esperando. Muchas gracias.

    Write your review!

    Write a review

    Convert decimal numbers to fractions

    Convert decimal numbers to fractions

    With this programming algorithm you can convert decimal numbers to fractions. Developed in VB.NET.

    Ask a question

    NO registration required!

    If the question you have has not yet been answered here, use the form below to ask something about this addon.

    *(Required to be notified when an answer is available)

    If the download link redirects to another product that is not described in the article or is broken, report it using our Reporting Form