Java :: 比较 ronded doubles 不起作用
Java :: comparing ronded doubles doesn't work
我想比较两个数字:
-3.123
-3.123456
我将使用 double
来存储它们,我只想考虑 前 3 位小数 ,所以:
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalComparator {
public static void main(String[] args) {
areEqualByThreeDecimalPlaces(-3.123, -3.123456);
}
public static boolean areEqualByThreeDecimalPlaces (double one, double two) {
boolean same = true;
DecimalFormat df = new DecimalFormat("#.###");
df.setRoundingMode(RoundingMode.FLOOR);
System.out.println(df.format(one));
System.out.println(df.format(two));
if (df.format(one).equals(df.format(two))) {
same = true;
System.out.println("true");
} else {
same = false;
System.out.println("false");
}
return same;
}
}
代码返回我:
-3.123
-3.124
false
为什么第二个数字四舍五入为-3.124?
RoundingMode.FLOOR
将数字向下舍入 - 您的代码适用于正数,但不适用于负数。您需要使用 RoundingMode.DOWN
只删除 N 位后的数字:
df.setRoundingMode(RoundingMode.DOWN);
// Here ------------------------^
Hi its just that when u round off 3.123456 it works the below way when decimal
places are reduced :
3.12346
3.1235
3.124
3.12
3.1
//commenting the set rounding mode will make it work the way u want.
public static void main (String[] args ) {
double one =-3.123;
double two = -3.123456;
boolean same = true;
DecimalFormat df = new DecimalFormat("#.###");
//remove this line
//df.setRoundingMode(RoundingMode.FLOOR);
System.out.println(df.format(one));
System.out.println(df.format(two));
if (df.format(one).equals(df.format(two))) {
same = true;
System.out.println("true");
} else {
same = false;
System.out.println("false");
}
System.out.println(same);
}
我想比较两个数字:
-3.123
-3.123456
我将使用 double
来存储它们,我只想考虑 前 3 位小数 ,所以:
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalComparator {
public static void main(String[] args) {
areEqualByThreeDecimalPlaces(-3.123, -3.123456);
}
public static boolean areEqualByThreeDecimalPlaces (double one, double two) {
boolean same = true;
DecimalFormat df = new DecimalFormat("#.###");
df.setRoundingMode(RoundingMode.FLOOR);
System.out.println(df.format(one));
System.out.println(df.format(two));
if (df.format(one).equals(df.format(two))) {
same = true;
System.out.println("true");
} else {
same = false;
System.out.println("false");
}
return same;
}
}
代码返回我:
-3.123
-3.124
false
为什么第二个数字四舍五入为-3.124?
RoundingMode.FLOOR
将数字向下舍入 - 您的代码适用于正数,但不适用于负数。您需要使用 RoundingMode.DOWN
只删除 N 位后的数字:
df.setRoundingMode(RoundingMode.DOWN);
// Here ------------------------^
Hi its just that when u round off 3.123456 it works the below way when decimal
places are reduced :
3.12346
3.1235
3.124
3.12
3.1
//commenting the set rounding mode will make it work the way u want.
public static void main (String[] args ) {
double one =-3.123;
double two = -3.123456;
boolean same = true;
DecimalFormat df = new DecimalFormat("#.###");
//remove this line
//df.setRoundingMode(RoundingMode.FLOOR);
System.out.println(df.format(one));
System.out.println(df.format(two));
if (df.format(one).equals(df.format(two))) {
same = true;
System.out.println("true");
} else {
same = false;
System.out.println("false");
}
System.out.println(same);
}