在Java中双重分数 [英] Double to fraction in Java

查看:174
本文介绍了在Java中双重分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想做的是将双重转换为理性数字。我检查小数点后有多少个数字,例如,我想将数字123.456保存为123456/1000。

  public Rational(double d){
String s = String.valueOf(d);
int digitsDec = s.length() - 1 - s.indexOf('。'); (int i = 0; i< digitsDec; i ++)

{
d * = 10;
}

System.out.println((int)d); //检查目的
}

然而,对于123.456的数字,我得到一个圆满的错误结果是123455.我想可以用BigDecimal来解决这个问题,但是我无法让它工作。另外,在计算了它的理性数量的时候,我想调用一个参数(int numerator,int分母)的另一个构造函数,但是我不能明确地调用println现在的行中的构造函数。我应该怎么做?

解决方案

对于问题的第一部分,Java存储.6为.5999999(重复) 。查看此输出:

 (第一次乘法后):d = 1234.56 
(第二次乘法后):d = 12345.599999999999
(第三乘以后):d = 123455.99999999999

一个修复是使用d = Math 。循环完成之后的.round(d)。

  public class Rational {

private int num ,denom;

public Rational(double d){
String s = String.valueOf(d);
int digitsDec = s.length() - 1 - s.indexOf('。');

int denom = 1; (int i = 0; i< digitsDec; i ++)
{
d * = 10;
denom * = 10;
}
int num =(int)Math.round(d);

this.num = num; this.denom = denom;
}

public Rational(int num,int denom){
this.num = num; this.denom = denom;
}

public String toString(){
return String.valueOf(num)+/+ String.valueOf(denom);
}

public static void main(String [] args){
System.out.println(new Rational(123.456));
}
}

它的工作原理 - 尝试。



对于您的问题的第二部分...



为了从第一个调用第二个构造函数,可以使用this关键字

 此(num,denom)

但它必须是构造函数中的第一行...这在这里没有意义(我们必须先做一些计算)。所以我不会打扰这样做。


So what I'm trying to do is convert double to rational number. I check how many digits there is after decimal point and I want to save the number 123.456 as 123456 / 1000, for example.

public Rational(double d){      
    String s = String.valueOf(d);
    int digitsDec = s.length() - 1 - s.indexOf('.');        

    for(int i = 0; i < digitsDec; i++){
        d *= 10;
    }

    System.out.println((int)d); //checking purposes
}   

However, for the number 123.456 I get a round off error and the result is 123455. I guess it'd be possible to fix this with BigDecimal but I can't get it to work. Also, having calculated what rational number it would be, I would like to call another constructor with parameters (int numerator, int denominator) but I can't obviously call the constructor in the line where println is now. How should I do this?

解决方案

For the first part of the question, Java is storing .6 as .5999999 (repeating). See this output:

(after first multiply): d=1234.56
(after second multiply): d=12345.599999999999
(after third multiply): d=123455.99999999999

One fix is to use d = Math.round(d) immediately after your loop finishes.

public class Rational {

     private int num, denom;

     public Rational(double d) {
          String s = String.valueOf(d);
          int digitsDec = s.length() - 1 - s.indexOf('.');        

          int denom = 1;
          for(int i = 0; i < digitsDec; i++){
             d *= 10;
             denom *= 10;
          }
          int num = (int) Math.round(d);

          this.num = num; this.denom = denom;
     }

     public Rational(int num, int denom) {
          this.num = num; this.denom = denom;
     }

     public String toString() {
          return String.valueOf(num) + "/" + String.valueOf(denom);
     }

     public static void main(String[] args) {
          System.out.println(new Rational(123.456));
     }
}

It works - try it.

For the second part of your question...

In order to call the second constructor from the first, you can use the "this" keyword

this(num, denom)

But it has to be the very first line in the constructor... which doesn't make sense here (we have to do some calculations first). So I wouldn't bother trying to do that.

这篇关于在Java中双重分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆