双在Java分数(Double to fraction in Java)

2019-07-17 12:47发布

所以我想要做的是double转换成有理数。 我检查有多少位有小数点后,我想数量123.456保存为千分之十二万三千四百五十六,例如。

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
}   

然而,对于数量123.456我得到一个舍入误差,结果是123455.我想这会是可能用BigDecimal的解决这个问题,但我不能得到它的工作。 此外,已经计算那会是有理数,我想调用的参数与另一个构造(INT分子,分母INT),但我不能明明调用构造函数中的行,其中的println就是现在。 我应该怎么办呢?

Answer 1:

对于问题的第一部分,爪哇正存储0.6为0.5999999(重复)。 看到以下输出:

(after first multiply): d=1234.56
(after second multiply): d=12345.599999999999
(after third multiply): 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;
          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));
     }
}

它的工作原理 - 尝试。

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

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

this(num, denom)

但它必须是在构造函数中的第一行......这没有任何意义在这里(我们必须首先做一些计算)。 所以,我就懒得努力做到这一点。



Answer 2:

此代码可能是矫枉过正你,但与你所遇到的舍入误差交易,它也需要照顾循环小数的(4.99999999999999变成5,和0.33333333333333333333变成1/3)。

public static Rational toRational(double number){
return toRational(number, 8);
}

public static Rational toRational(double number, int largestRightOfDecimal){

long sign = 1;
if(number < 0){
    number = -number;
    sign = -1;
}

final long SECOND_MULTIPLIER_MAX = (long)Math.pow(10, largestRightOfDecimal - 1);
final long FIRST_MULTIPLIER_MAX = SECOND_MULTIPLIER_MAX * 10L;
final double ERROR = Math.pow(10, -largestRightOfDecimal - 1);
long firstMultiplier = 1;
long secondMultiplier = 1;
boolean notIntOrIrrational = false;
long truncatedNumber = (long)number;
Rational rationalNumber = new Rational((long)(sign * number * FIRST_MULTIPLIER_MAX), FIRST_MULTIPLIER_MAX);

double error = number - truncatedNumber;
while( (error >= ERROR) && (firstMultiplier <= FIRST_MULTIPLIER_MAX)){
    secondMultiplier = 1;
    firstMultiplier *= 10;
    while( (secondMultiplier <= SECOND_MULTIPLIER_MAX) && (secondMultiplier < firstMultiplier) ){
        double difference = (number * firstMultiplier) - (number * secondMultiplier);
        truncatedNumber = (long)difference;
        error = difference - truncatedNumber;
        if(error < ERROR){
            notIntOrIrrational = true;
            break;
        }
        secondMultiplier *= 10;
    }
}

if(notIntOrIrrational){
    rationalNumber = new Rational(sign * truncatedNumber, firstMultiplier - secondMultiplier);
}
return rationalNumber;
}

这提供了以下结果(来自测试案例结果显示为注释):

Rational.toRational(110.0/3.0); // 110/3
Rational.toRational(11.0/1000.0); // 11/1000
Rational.toRational(17357.0/33300.0); // 17357/33300
Rational.toRational(215.0/21.0); // 215/21
Rational.toRational(0.123123123123123123123123); // 41/333
Rational.toRational(145731.0/27100.0); // 145731/27100
Rational.toRational(Math.PI); // 62831853/20000000
Rational.toRational(62.0/63.0); // 62/63
Rational.toRational(24.0/25.0); // 24/25
Rational.toRational(-24.0/25.0); //-24/25
Rational.toRational(-0.25333333333333333333333); // -19/75
Rational.toRational(-4.9999999999999999999999); // -5
Rational.toRational(4.9999999999999999999999);  // 5
Rational.toRational(123.456); // 15432/125


Answer 3:

这是不优雅,但是,我相信这样做你问。

double a = 123.456;
String aString = Double.toString(a);        
String[] fraction = aString.split("\\.");

int denominator = (int)Math.pow(10, fraction[1].length());
int numerator = Integer.parseInt(fraction[0] + "" + fraction[1]);

System.out.println(numerator + "/" + denominator);


Answer 4:

在此,d = 123.456然后NUM = 123456,J = 1000

public Rational(double d){      
  double temp =d;
  int j=1, num;
  do{
     j=j*10;
     }while((temp*j)%10==0);        
  j=j/10;
  num=(int)(d*j);
  System.out.println(num);
  System.out.println(j);
}   


Answer 5:

尝试

for(int i = 0; i <= digitsDec; i++){

}


文章来源: Double to fraction in Java