What I need to convert decimal to fractions. It is easy to convert to 10\'s feet.
1.5 => 15/10
This can do via this code:
Including the method to find highest common factor and modifying toString method, solves your question i suppose.
public String toString() {
int hcf = findHighestCommonFactor(num, denom);
return (String.valueOf(num/hcf) + "/" + String.valueOf(denom/hcf));
}
private int findHighestCommonFactor(int num, int denom) {
if (denom == 0) {
return num;
}
return findHighestCommonFactor(denom, num % denom);
}
Well check this simple implementation, I have not used any GCD or something, instead, I have put the logic for the numerator and keep on incrementing till the logic is not fulfilled.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the decimal number:");
double d = scan.nextDouble();
int denom = 1;
boolean b = true;
while(b) {
String[] s = String.valueOf(d * denom).split("\\.");
if(s[0].equals(String.valueOf((int)(d * denom))) && s[1].equals("0")) {
break;
}
denom++;
}
if(denom == 1) {
System.out.println("Input a decimal number");
}
else {
System.out.print("Fraction: ");
System.out.print((int)(d*denom)+"/"+denom);
}
}
Given double x >= 0, int p, int q, find p/q as closest approximation:
So (not tested):
public static Rational toFraction(double x) {
// Approximate x with p/q.
final double eps = 0.000_001;
int pfound = (int) Math.round(x);
int qfound = 1;
double errorfound = Math.abs(x - pfound);
for (int q = 2; q < 100 && error > eps; ++q) {
int p = (int) (x * q);
for (int i = 0; i < 2; ++i) { // below and above x
double error = Math.abs(x - ((double) p / q));
if (error < errorfound) {
pfound = p;
qfound = q;
errorfound = error;
}
++p;
}
}
return new Rational(pfound, qfound);
}
You could try it for Math.PI and E.
I prepared a solution for this question. Maybe it's look like primitive but working. I tested many decimal number. At least it can converting 1.5 to 3/2 :)
public String kesirliYap(Double sayi){
String[] a=payPaydaVer(sayi);
return a[0]+"/"+a[1];
}
public String[] payPaydaVer(Double sayi){
long pay;
long payda;
DecimalFormat df=new DecimalFormat("#");
df.setRoundingMode(RoundingMode.FLOOR);
String metin=sayi.toString();
int virguldenSonra=(metin.length() -metin.indexOf("."))-1;
double payyda=Math.pow(10,virguldenSonra);
double payy=payyda*sayi;
String pays=df.format(payy);
String paydas=df.format(payyda);
pay=Long.valueOf(pays);
payda=Long.valueOf(paydas);
String[] kesir=sadelestir(pay,payda).split(",");
return kesir;
}
private String sadelestir(Long pay,Long payda){
DecimalFormat df=new DecimalFormat("#");
df.setRoundingMode(RoundingMode.FLOOR);
Long a=pay<payda ? pay : payda;
String b = "",c = "";
int sayac=0;
for(double i = a;i>1;i--){
double payy=pay/i;
double paydaa=payda/i;
String spay=df.format(payy);
String spayda=df.format(paydaa);
Long lpay=Long.valueOf(spay);
Long lpayda=Long.valueOf(spayda);
if((payy-lpay)==0&&(paydaa-lpayda)==0){
b=df.format(pay/i);
c=df.format(payda/i);
sayac++;
break;
}
}
return sayac>0 ? b+","+c:pay+","+payda;
}