BigDecimal 中
比如 a=1.0    b=1.0000;
输出a+b    为   2.0000  
结果的精度和a b中精度比较高的一个相同;现在我想去掉后置0  即输出2 
请教各位大牛,如何办本题是acm题
http://acm.hdu.edu.cn/showproblem.php?pid=1753下面是不考虑输出精度的代码, 在这个基础上修改即可
import java.io.*;
import java.math.BigDecimal;
import java.util.Scanner;public class Main {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
BigDecimal a, b;
while(in.hasNext()) {
a = in.nextBigDecimal();
b = in.nextBigDecimal();
System.out.println(a.add(b));
}
}
}

解决方案 »

  1.   

    BigDecimal没研究过,转换成字符串可以吧import java.io.IOException;
    import java.math.BigDecimal;
    import java.util.Scanner;public class Main {
        public static void main(String[] args) throws IOException {
            Scanner in = new Scanner(System.in);
            BigDecimal a, b;
            while(in.hasNext()) {
                a = in.nextBigDecimal();
                b = in.nextBigDecimal();
                String temp = String.valueOf(a.add(b));
                while(temp.endsWith("0") && temp.indexOf(".") != -1){
                 temp = temp.substring(0, temp.length() - 1);
                 if(temp.endsWith("."))
                 temp = temp.substring(0, temp.length() - 1);
                }
                System.out.println(temp);
            }
        }
    }
      

  2.   

    如果光API的话,BigDecimal.stripTrailingZeros()如果算法的话,判断保留i=0 to N位小数后的结果与原数的值是否相等(compareTo,而不是equals)
      

  3.   

    还不能结
    应该楼上的两种方法都可以,但是只有第二种(4楼的那种方法)可以ac,原因我也找出来了
    用api BigDecimal.stripTrailingZeros()  处理 600.00 + 0.00 时输出的是 6E+2 而不是600现有两个问题,
    1,如果一定要用api BigDecimal.stripTrailingZeros() ,输出600 怎么写;
    2.请帮忙翻译(直译) api 文档
    For example, stripping the trailing zeros from the BigDecimal value 600.0, which has [BigInteger, scale] components equals to [6000, 1], yields 6E2 with [BigInteger, scale] components equals to [6, -2] 
    谢谢
      

  4.   

    1 lz看到的是6E2,也就是科学计数法, 6*10^2,这个只是format问题,精确度中已经没有了小数点后面的0,要格式输出的话,可以用,
    System.out.println(new BigDecimal("600.00").stripTrailingZeros().toPlainString());
    或下面的代码得到类似new BigDecimal("600")的效果:
    new BigDecimal(new BigDecimal("600.00").stripTrailingZeros().toPlainString());2 例如,BigDecimal类型600.00,等价于有效数字(不含小数点,译注)6000,精确度1(保留一位小数,译注),去除它结尾的0,得到的结果是6E2,也就是有效数字6,精确度-2(精确到小数点前2位,即百位,译注)
      

  5.   

    勘误
    2 例如,BigDecimal类型600.0,等价于有效数字(不含小数点,译注)6000,精确度1(保留一位小数,译注)的BigInteger,去除它结尾的0,得到的结果是6E2,也就是有效数字6,精确度-2(精确到小数点前2位,即百位,译注)