当前位置: 技术问答>java相关
大虾帮忙啊!高分请教,在线等待,立即给分...
来源: 互联网 发布时间:2017-04-21
本文导语: 大虾们看一下下面的代码: double f = 12.121; double s = java.lang.Math.round(f); double t = f-s; System.out.println(f); System.out.println(s); System.out.println(t); 输出结果是: 12.121 12.0 0.12100000000000044 这是为...
大虾们看一下下面的代码:
double f = 12.121;
double s = java.lang.Math.round(f);
double t = f-s;
System.out.println(f);
System.out.println(s);
System.out.println(t);
输出结果是:
12.121
12.0
0.12100000000000044
这是为什么呢?怎么解决?能不能说的详细一点。
还有用float时也一样
double f = 12.121;
double s = java.lang.Math.round(f);
double t = f-s;
System.out.println(f);
System.out.println(s);
System.out.println(t);
输出结果是:
12.121
12.0
0.12100000000000044
这是为什么呢?怎么解决?能不能说的详细一点。
还有用float时也一样
|
需要知道精确结果, 不能用float或者double. 用int, 或者BigDecimal, 下面是一个小例子: 计算1元钱钱可以买多少糖果. 其中糖果一毛钱一个.
//错误的, 使用double型
public class InappropriateFloat {
public static void main(String[] args) {
double funds = 1.00;
int itemsBought = 0;
for (double price = .10; funds >= price; price += .10) {
funds -= price;
itemsBought++;
}
System.out.println(itemsBought + " items bought.");
System.out.println("Change: ¥" + funds);
}
}
//使用整型, 用分计算
public class IntVersion {
public static void main(String[] args) {
int itemsBought = 0;
int funds = 100;
for (int price = 10; funds >= price; price += 10) {
itemsBought++;
funds -= price;
}
System.out.println(itemsBought + " items bought.");
System.out.println("Money left over: "+ funds + " cents");
}
}
//用BigDecimal计算
import java.math.BigDecimal;
public class BigDecimalVersion {
public static void main(String[] args) {
final BigDecimal TEN_CENTS = new BigDecimal( ".10");
int itemsBought = 0;
BigDecimal funds = new BigDecimal("1.00");
for (BigDecimal price = TEN_CENTS;
funds.compareTo(price) >= 0;
price = price.add(TEN_CENTS)) {
itemsBought++;
funds = funds.subtract(price);
}
System.out.println(itemsBought + " items bought.");
System.out.println("Money left over: ¥" + funds);
}
}
这是Java高效编程上的例子。
我测试过没有问题,你试下~~
//错误的, 使用double型
public class InappropriateFloat {
public static void main(String[] args) {
double funds = 1.00;
int itemsBought = 0;
for (double price = .10; funds >= price; price += .10) {
funds -= price;
itemsBought++;
}
System.out.println(itemsBought + " items bought.");
System.out.println("Change: ¥" + funds);
}
}
//使用整型, 用分计算
public class IntVersion {
public static void main(String[] args) {
int itemsBought = 0;
int funds = 100;
for (int price = 10; funds >= price; price += 10) {
itemsBought++;
funds -= price;
}
System.out.println(itemsBought + " items bought.");
System.out.println("Money left over: "+ funds + " cents");
}
}
//用BigDecimal计算
import java.math.BigDecimal;
public class BigDecimalVersion {
public static void main(String[] args) {
final BigDecimal TEN_CENTS = new BigDecimal( ".10");
int itemsBought = 0;
BigDecimal funds = new BigDecimal("1.00");
for (BigDecimal price = TEN_CENTS;
funds.compareTo(price) >= 0;
price = price.add(TEN_CENTS)) {
itemsBought++;
funds = funds.subtract(price);
}
System.out.println(itemsBought + " items bought.");
System.out.println("Money left over: ¥" + funds);
}
}
这是Java高效编程上的例子。
我测试过没有问题,你试下~~