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));
}
}
}
比如 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));
}
}
}
解决方案 »
- 关于类的实例变量的默认参数和构造函数的疑问
- DefaultStyledDocument.setCharacterAttributes()
- 网页java 代码 改成 VB
- 如何隐藏Runtime.getRuntime().exec(String)之后的Command Line Window?
- Java怎样实现图片跟随窗口的大小而变动?
- <<think in java>>里的关于垃圾回收的问题?垃圾回收器也有失灵的时候?
- 一个jTree控件有若干个层次, 怎样向某一层添加一个节点.
- java如何判断两个字符串中有几处不同的地方
- java初级问题
- java扫描线种子填充的问题
- 哪们懂js 可不可以反这个 js 用java 写出来呀,帮帮忙!
- 请问一下为什么这个APPLET总是闪烁??谢谢
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);
}
}
}
应该楼上的两种方法都可以,但是只有第二种(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]
谢谢
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位,即百位,译注)
2 例如,BigDecimal类型600.0,等价于有效数字(不含小数点,译注)6000,精确度1(保留一位小数,译注)的BigInteger,去除它结尾的0,得到的结果是6E2,也就是有效数字6,精确度-2(精确到小数点前2位,即百位,译注)