title: Double to fraction in Java
body: <p>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. </p>
  
  <pre><code>public Rational(double d){      
      String s = String.valueOf(d);
      int digitsDec = s.length() - 1 - s.indexOf('.');        
  
      for(int i = 0; i &lt; digitsDec; i++){
          d *= 10;
      }
  
      System.out.println((int)d); //checking purposes
  }   
  </code></pre>
  
  <p>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?</p>