double是一个不准确的类型 用BigDecimal类类似代码有public class While1{
public static void main(String[] args){
double a=3.0;
double b=2.9;
System.out.println(a-b);
}
}
输出结果0.10000000000000009
public static void main(String[] args){
double a=3.0;
double b=2.9;
System.out.println(a-b);
}
}
输出结果0.10000000000000009
解决方案 »
- JAVA 如何调用多结果集的存储过程
- java Soa 的soap协议的定义是什么
- 100分解决异常错误:org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling s
- 问个十分愚蠢的问题,浏览器向服务端发送数据,是不是只有表单和url这两种?
- 求一算法 兄弟们进来看看...愁死我了
- 问一个美国cs研究生作业
- 今天刚学到函数,有个问题弄不懂
- 一个数值三种表达方式的不同在哪里?
- 如何写中间件方面的毕设论文
- 如何import自己的package,用自己写的类 ?一定给分
- 一个空指针异常问题
- TreeMap的一个小问题
import java.math.BigDecimal;
class while2{
public static void main(String[] args){
//总共的钱,每天存的钱,每五天用点钱
。。
while(true){
。
if(day%5==0){
sum-=withdraw;
}
BigDecimal bd = new BigDecimal(sum);
bd = bd.setScale(2,BigDecimal.ROUND_HALF_UP);//精确到小数点后两位,四舍五入
System.out.println(bd+"---"+day);
}
。
}
}
/*第二种方法*/
import java.text.NumberFormat;
class while2{
public static void main(String[] args){
。
while(true){
。。
NumberFormat nfm = NumberFormat.getInstance();
nfm.setMaximumFractionDigits(2);//设置数值的小数部分允许的最大位数
nfm.setMinimumFractionDigits(2);//设置数值的小数部分允许的最小位数
System.out.println(nfm.format(sum)+"---"+day);
}
。。
}
}