英文:
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?
英文:
I want to compare two numbers:
-3.123
-3.123456
I will use double
to store them and I just want to consider the first 3 decimals, so:
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;
}
}
The code is returning me:
-3.123
-3.124
false
why the second number is rounding to -3.124?
答案1
得分: 1
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);
}
英文:
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);
}
答案2
得分: 0
RoundingMode.FLOOR
会将数字向下舍入 - 您的代码对于正数可以工作,但对于负数则不行。您需要使用 RoundingMode.DOWN
,它只是在第 N 位之后删除数字:
df.setRoundingMode(RoundingMode.DOWN);
// 在这里 ------------------------^
英文:
RoundingMode.FLOOR
rounds the number towards the lower side - your code would work for positive numbers, but not for negative numbers. You need to use RoundingMode.DOWN
that just removes digits after N places:
df.setRoundingMode(RoundingMode.DOWN);
// Here ------------------------^
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论