日期:2014-05-20  浏览次数:20817 次

帮我看看这个问题,谢谢
import   javax.swing.JOptionPane;
public   class   yuan{
  public   static   void   main(String   args[])
  {
      String   Number;

     
      double   r,d,c,s;
     
       
      Number=JOptionPane.showInputDialog( "Enter   the   integer ");
     
   

      r=Double.parseDouble(Number);
     
   
      d=2*r;
     

      c=2*3.14*r;

      s=3.14*r*r;
     

      JOptionPane.showMessageDialog(
          null, "The   d   is   "+d, "Results ",JOptionPane.PLAIN_MESSAGE);
     

      JOptionPane.showMessageDialog(
          null, "The   c   is   "+c, "Results ",JOptionPane.PLAIN_MESSAGE);
       

      JOptionPane.showMessageDialog(
          null, "The   s   is   "+s, "Results ",JOptionPane.PLAIN_MESSAGE);
         

 

      System.exit(0);
    }
}  

输入4以后得到的结果是:25.12,这个没问题;
但是输入5以后得到的结果却是:31.00000000000002.
为什么呢?

------解决方案--------------------
先看看这里吧..
http://community.csdn.net/Expert/topic/5731/5731238.xml?temp=.5735437
------解决方案--------------------
小数计算要用BigDecimal类型,否则会有误差。而且在new对象时一定要用new BigDecimal(String value)的这个构造器,否则也会有误差。