Java::比较舍入的双精度浮点数不起作用。

huangapple go评论106阅读模式
英文:

Java :: comparing ronded doubles doesn't work

问题

我想比较两个数字:

  • -3.123
  • -3.123456

我将使用 double 来存储它们,我只想考虑前3位小数,所以:

  1. import java.math.RoundingMode;
  2. import java.text.DecimalFormat;
  3. public class DecimalComparator {
  4. public static void main(String[] args) {
  5. areEqualByThreeDecimalPlaces(-3.123, -3.123456);
  6. }
  7. public static boolean areEqualByThreeDecimalPlaces(double one, double two) {
  8. boolean same = true;
  9. DecimalFormat df = new DecimalFormat("#.###");
  10. df.setRoundingMode(RoundingMode.FLOOR);
  11. System.out.println(df.format(one));
  12. System.out.println(df.format(two));
  13. if (df.format(one).equals(df.format(two))) {
  14. same = true;
  15. System.out.println("true");
  16. } else {
  17. same = false;
  18. System.out.println("false");
  19. }
  20. return same;
  21. }
  22. }

这段代码返回给我:

  1. -3.123
  2. -3.124
  3. 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:

  1. import java.math.RoundingMode;
  2. import java.text.DecimalFormat;
  3. public class DecimalComparator {
  4. public static void main(String[] args) {
  5. areEqualByThreeDecimalPlaces(-3.123, -3.123456);
  6. }
  7. public static boolean areEqualByThreeDecimalPlaces (double one, double two) {
  8. boolean same = true;
  9. DecimalFormat df = new DecimalFormat("#.###");
  10. df.setRoundingMode(RoundingMode.FLOOR);
  11. System.out.println(df.format(one));
  12. System.out.println(df.format(two));
  13. if (df.format(one).equals(df.format(two))) {
  14. same = true;
  15. System.out.println("true");
  16. } else {
  17. same = false;
  18. System.out.println("false");
  19. }
  20. return same;
  21. }
  22. }

The code is returning me:

  1. -3.123
  2. -3.124
  3. false

why the second number is rounding to -3.124?

答案1

得分: 1

  1. Hi its just that when u round off 3.123456 it works the below way when decimal
  2. places are reduced :
  3. 3.12346
  4. 3.1235
  5. 3.124
  6. 3.12
  7. 3.1
  8. // commenting the set rounding mode will make it work the way u want.
  9. public static void main (String[] args ) {
  10. double one =-3.123;
  11. double two = -3.123456;
  12. boolean same = true;
  13. DecimalFormat df = new DecimalFormat("#.###");
  14. // remove this line
  15. // df.setRoundingMode(RoundingMode.FLOOR);
  16. System.out.println(df.format(one));
  17. System.out.println(df.format(two));
  18. if (df.format(one).equals(df.format(two))) {
  19. same = true;
  20. System.out.println("true");
  21. } else {
  22. same = false;
  23. System.out.println("false");
  24. }
  25. System.out.println(same);
  26. }
英文:
  1. Hi its just that when u round off 3.123456 it works the below way when decimal
  2. places are reduced :
  3. 3.12346
  4. 3.1235
  5. 3.124
  6. 3.12
  7. 3.1
  8. //commenting the set rounding mode will make it work the way u want.
  9. public static void main (String[] args ) {
  10. double one =-3.123;
  11. double two = -3.123456;
  12. boolean same = true;
  13. DecimalFormat df = new DecimalFormat("#.###");
  14. //remove this line
  15. //df.setRoundingMode(RoundingMode.FLOOR);
  16. System.out.println(df.format(one));
  17. System.out.println(df.format(two));
  18. if (df.format(one).equals(df.format(two))) {
  19. same = true;
  20. System.out.println("true");
  21. } else {
  22. same = false;
  23. System.out.println("false");
  24. }
  25. System.out.println(same);
  26. }

答案2

得分: 0

RoundingMode.FLOOR会将数字向下舍入 - 您的代码对于正数可以工作,但对于负数则不行。您需要使用 RoundingMode.DOWN ,它只是在第 N 位之后删除数字:

  1. df.setRoundingMode(RoundingMode.DOWN);
  2. // 在这里 ------------------------^
英文:

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:

  1. df.setRoundingMode(RoundingMode.DOWN);
  2. // Here ------------------------^

huangapple
  • 本文由 发表于 2020年4月10日 16:44:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/61136849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定