import java.util.Scanner; public class ChangeToFenshuDemo { public static int getGongYueShu(int a, int b) {//求两个数的最大公约数 int t = 0; if(a < b){ t = a; a = b; b = t; } int c = a % b; if(c == 0){ return b; }else{ return getGongYueShu(b, c); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("input xiaoshu"); String xiaoshu = in.next(); String[] array = new String[2]; array = xiaoshu.split("\\."); int a = Integer.parseInt(array[0]);//获取整数部分 int b = Integer.parseInt(array[1]);//获取小数部分 int length = array[1].length(); int FenZi = (int) (a * Math.pow(10, length) + b); int FenMu = (int) Math.pow(10, length); int MaxYueShu = getGongYueShu(FenZi, FenMu); System.out.println(FenZi / MaxYueShu + "/" + FenMu / MaxYueShu); } }
原文地址:http://blog.csdn.net/happy_girl2012/article/details/46418261