java实现小数转分数
public static String dicimalToFraction(double num){
int count = 0;
int base = 10;
while(num != Math.floor(num)){
num *= base;
count++;
}
base = (int)Math.pow(base,count);
int nor = (int)num;
int gcd = findGCD(nor, base);
return String.valueOf(nor/gcd) + "/" + String.valueOf(base/gcd);
}
//求最大公约数
private static int findGCD(int a, int b){
if(a == 0){
return b;
}
return findGCD(b%a, a);
}