用double计算也只能精确到小数点后16位。我要提高精确度,比如说小数点后32位。怎么办?我用BigDecimal解决精度缺失问题。这也要考虑在内。DecimalFormat似乎不行。只是在16位后加几个0罢了。最终要得到的效果就像:1 / 3 = 0.33333333333333333333333333333333 (32个3)

解决方案 »

  1.   

    BigDecimal 完全可以解决任何精度问题,所有+ - * / 问题都通过方法来操作,如果要显示,就转成字符串显示即可,如果你要转成double,肯定会缺失。
      

  2.   

    BigDecimal 由任意精度的整数非标度值 和 32 位的整数标度 (scale) 组成。如果为零或正数,则标度是小数点后的位数。如果为负数,则将该数的非标度值乘以 10 的负 scale 次幂。因此,BigDecimal 表示的数值是 (unscaledValue × 10-scale)。 可以看见,可以处理的小数位数起码是10^31位
      

  3.   

    不过如果你知道怎么计算,可以使用1来构造一个bigdecimal 3来构造一个bigdecimal,然后两个通过运算进行相除,就可以得到你要的精度
      

  4.   

    知道了。设一个scale的值就行了。
    其实它有那么智能的
      

  5.   

    import java.text.*;
    import java.util.*;public class DecimalFormatSample {
     public static void main(String args[]) {
      DecimalFormat myformat1 = new DecimalFormat("###,###.0000");//使用系统默认的格式
      System.out.println(myformat1.format(111111123456.12));  Locale.setDefault(Locale.US);
      DecimalFormat myformat2 = new DecimalFormat("###,###.0000");//使用美国的格式
      System.out.println(myformat2.format(111111123456.12));  //----------------------------also use applypattern------------------------------//  DecimalFormat myformat3 = new DecimalFormat();
      myformat3.applyPattern("##,###.000");
      System.out.println(myformat3.format(11112345.12345));
    //-----------------控制指数输出-------------------------------------------------//     DecimalFormat myformat4 = new DecimalFormat();
      myformat4.applyPattern("0.000E0000");
      System.out.println(myformat4.format(10000));
      System.out.println(myformat4.format(12345678.345));
    //------------------百分数的输出-------------------------------------------//
    /*     DecimalFormat是NumberFormat的一个子类,其实例被指定为特定的地区。因此,你可以使用NumberFormat.getInstance 指定一个地区,
    然后将结构强制转换为一个DecimalFormat对象。文档中提到这个技术可以在大多情况下适用,但是你需要用try/catch 块包围强制转换以防转
    换不能正常工作 (大概在非常不明显得情况下使用一个奇异的地区)。    */
           DecimalFormat myformat5 = null;
      try{
          myformat5 = (DecimalFormat)NumberFormat.getPercentInstance();
      }catch(ClassCastException e)
      {
       System.err.println(e); 
      }
      myformat5.applyPattern("00.0000%");
      System.out.println(myformat5.format(0.34567));
      System.out.println(myformat5.format(1.34567));       
     
     }/*---------------------------------运行结果-------------------------------------------//
     F:\2004-04-12>java DecimalFormatSample
    111,111,123,456.1200
    111,111,123,456.1200
    11,112,345.123
    1.000E0004
    1.235E0007
    34.5670%
    134.5670%*/}
      

  6.   

    public static void main(String[] args) {        BigDecimal bd = new BigDecimal("0.33333333333333333333").setScale(20);
            System.out.println(bd.toString());
            System.out.println(bd.toString().length());
        }
      

  7.   

    public static void main(String[] args) {        BigDecimal bd = new BigDecimal("0.33333333333333333333333333333333").setScale(32);
            System.out.println(bd.toString());
            System.out.println("小数位数:" + (bd.toString().length() - 2));
        }
      

  8.   

    答:楼主,我来写个代码,就像楼主要的那个(我们来个34个3,比你要的还多2个3。当然,也可以任意精度):
    最终要得到的效果就像:1 / 3 = 0.33333333333333333333333333333333 (32个3)
    参考代码如下:import java.math.*;
    public class BigDecimalNumber {
    public static void main(String[] args) {
    BigDecimal b1=new BigDecimal("1",MathContext.DECIMAL128);
    BigDecimal b2=new BigDecimal("3",MathContext.DECIMAL128);
    BigDecimal result = b1.divide(b2,MathContext.DECIMAL128);// 1/3
    System.out.println(result);
    }
    }
    运行结果:0.3333333333333333333333333333333333