0% found this document useful (0 votes)
30 views2 pages

Untitled 1

The document describes a Java program that converts decimal numbers to fractions. It contains two classes: Main contains the main method that gets a decimal number from the user and calls the ConvertDecimal method. Perfrac contains methods to convert the decimal to a fraction, including getting the integer and decimal parts, then iterating through different numerator/denominator combinations to find the closest fraction within a precision level. It returns the fraction as a string.

Uploaded by

Andrew Nb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

Untitled 1

The document describes a Java program that converts decimal numbers to fractions. It contains two classes: Main contains the main method that gets a decimal number from the user and calls the ConvertDecimal method. Perfrac contains methods to convert the decimal to a fraction, including getting the integer and decimal parts, then iterating through different numerator/denominator combinations to find the closest fraction within a precision level. It returns the fraction as a string.

Uploaded by

Andrew Nb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package numeros;

import java.util.Scanner;

public class Main {


public static void main(String[] args) {

Scanner myObj = new Scanner(System.in);

System.out.println("Escriba el número periodico que desea convertir


a fracción:");

double n = myObj.nextDouble();

Perfrac Conversion= new Perfrac();

String resultado= Conversion.ConvertirDecimal(n);

System.out.println("El numero en forma fraccionaria es:\n"+


resultado);

}
}

package numeros;

public class Perfrac {

private double numeroaconvertir;


private int precDec=10000;

public double getNumeroaconvertir() {


return numeroaconvertir;
}

public void setNumeroaconvertir(double numeroaconvertir) {


this.numeroaconvertir = numeroaconvertir;
}

public int getPrecDec() {


return precDec;
}

public void setPrecDec(int precDec) {


this.precDec = precDec;
}

public String ConvertirDecimal(double numeroaconvertir){

int numeroentero = (int)numeroaconvertir;


double valordecimal = numeroaconvertir - numeroentero;

double diferencia = 1;
int numerador = 1;
int denominador = 1;

for (int y = 2; y < precDec + 1; y++)


{
for (int x = 1; x < y; x++)
{
double tempdif = Math.abs(valordecimal - (double)x /
(double)y);

if (tempdif < diferencia)


{
numerador = x;
denominador = y;
diferencia = tempdif;
if (diferencia == 0);
{

return Fraccion(numeroentero, numerador,


denominador);
}

}
}
return Fraccion(numeroentero, numerador, denominador);
}

public static String Fraccion(int numeroentero, int numerador, int


denominador)
{
if (numeroentero == 0)
{
return numerador + "/" + denominador;
}

else

{
int numerador1= ((numeroentero*denominador)+numerador);

// return numeroentero + " " + numerador + "/" + denominador;

return numerador1 + "/" + denominador;


}
}

You might also like