How to convert decimal to fractions?

前端 未结 10 2184
心在旅途
心在旅途 2020-12-06 06:26

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:



        
相关标签:
10条回答
  • 2020-12-06 07:16

    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);
        }
    
    0 讨论(0)
  • 2020-12-06 07:17

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-06 07:22

    Given double x >= 0, int p, int q, find p/q as closest approximation:

    • iterate on q from 1 upwards, determine p above and below; check deviations

    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.

    0 讨论(0)
  • 2020-12-06 07:24

    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;
    }
    
    0 讨论(0)
提交回复
热议问题