使用BigDecimals对区域进行四舍五入

问题描述 投票:0回答:1

我们正在做以下练习。TDD区域计算.

我们写了以下代码。

import java.math.*;
public class Calculator{
  public double getTotalArea(Triangle triangle){
    double area = triangle.base*triangle.height/2;
    System.out.println("getTotalArea of triangle: "+area);
    return area;
  }    

  public double getTotalArea(Square square){
    double area = BigDecimal.valueOf(square.side*square.side).setScale(2,BigDecimal.ROUND_HALF_EVEN).doubleValue();
    System.out.println("getTotalArea of square: "+area);
    return area;
  }      

  public double getTotalArea(Rectangle rectangle){
    double area = rectangle.width*rectangle.height;
    System.out.println("getTotalArea of rectangle: "+area);
    return area;
  }

  public double getTotalArea(Circle circle){
    double area = BigDecimal.valueOf(circle.radius*circle.radius*Math.PI).setScale(2,BigDecimal.ROUND_HALF_EVEN).doubleValue();
    System.out.println("getTotalArea of circle: "+area);
    return area;
  }

  public double getTotalArea(Shape ...shapes){
    Calculator calc = new Calculator();
    BigDecimal result= BigDecimal.ZERO;

    for(Shape shape : shapes){
      if(shape instanceof Triangle){
        result = result.add(BigDecimal.valueOf(calc.getTotalArea(new Triangle(shape.base,shape.height))));
        System.out.println("getTotalArea adding 📐: "+result);
      }
      if(shape instanceof Square){
        result=result.add(BigDecimal.valueOf(calc.getTotalArea(new Square(shape.side))));
        System.out.println("getTotalArea adding 🔲: "+result);
      }
      if(shape instanceof Rectangle){
        result=result.add(BigDecimal.valueOf(calc.getTotalArea(new Rectangle(shape.height,shape.width))));
        System.out.println("getTotalArea adding 🧱: "+result);
      }
      if(shape instanceof Circle){
        result=result.add(BigDecimal.valueOf(calc.getTotalArea(new Circle(shape.radius))));
        System.out.println("getTotalArea adding 🔵: "+result);
      }
    }
    System.out.println("getTotalArea of all shapes result: "+result+"\n\n");
    return result.setScale(2,BigDecimal.ROUND_HALF_EVEN).doubleValue();
  }

}

class Shape{
  double base, height, side, width, radius;    
}

class Triangle extends Shape{
  Triangle(double base, double height){
    this.base=base;
    this.height=height;
  }
}

class Square extends Shape{
  Square(double side){
    this.side=side;
  }  
}

class Rectangle extends Shape{
  Rectangle(double height, double width){
    this.height=height;
    this.width=width;
  }
}

class Circle extends Shape{
  Circle(double radius){
    this.radius=radius;
  }
}

我们想知道为什么在所有形状的getTotalArea方法中 会出现取整困难。 例如,让我们看看下面的跟踪。

getTotalArea of square: 1425.6
getTotalArea of square: 1425.6
getTotalArea adding 🔲: 1425.6
getTotalArea of circle: 31.92
getTotalArea adding 🔵: 1457.52
getTotalArea of all shapes result: 1457.52

expected:<1457.53> but was:<1457.52>

看起来当有正方形和圆形的时候 这个方法输出的数字和预期的不同 我们认为这是因为圆和方的方法给出了一个已经四舍五入的结果。

但是,测试要求将圆和方的结果四舍五入到两位小数。例如,如果我们改变方法如下。

  public double getTotalArea(Circle circle){
    double area = circle.radius*circle.radius*Math.PI;
    //double area = BigDecimal.valueOf(circle.radius*circle.radius*Math.PI).setScale(2,BigDecimal.ROUND_HALF_EVEN).doubleValue();
    System.out.println("getTotalArea of circle: "+area);
    return area;
  }

测试的结果是:

getTotalArea of circle: 28.274333882308138
expected:<28.27> but was:<28.274333882308138>

另外,我们还尝试用字符串将结果四舍五入。

import java.math.*;
public class Calculator{
  public double getTotalArea(Triangle triangle){
    double area = triangle.base*triangle.height/2;
    System.out.println("getTotalArea of triangle: "+area);
    return area;
  }    

  public double getTotalArea(Square square){
    double area = BigDecimal.valueOf(square.side*square.side).setScale(2,BigDecimal.ROUND_HALF_EVEN).doubleValue();
    System.out.println("getTotalArea of square: "+area);
    return area;
  }      

  public double getTotalArea(Rectangle rectangle){
    double area = rectangle.width*rectangle.height;
    System.out.println("getTotalArea of rectangle: "+area);
    return area;
  }

  public double getTotalArea(Circle circle){
    double area = BigDecimal.valueOf(circle.radius*circle.radius*Math.PI).setScale(2,BigDecimal.ROUND_HALF_EVEN).doubleValue();
    System.out.println("getTotalArea of circle: "+area);
    return area;
  }

  public double getTotalArea(Shape ...shapes){
    Calculator calc = new Calculator();
    BigDecimal result= BigDecimal.ZERO;

    for(Shape shape : shapes){
      if(shape instanceof Triangle){
        result = result.add(BigDecimal.valueOf(calc.getTotalArea(new Triangle(shape.base,shape.height))));
        System.out.println("getTotalArea adding 📐: "+result);
      }
      if(shape instanceof Square){
        result=result.add(BigDecimal.valueOf(calc.getTotalArea(new Square(shape.side))));
        System.out.println("getTotalArea adding 🔲: "+result);
      }
      if(shape instanceof Rectangle){
        result=result.add(BigDecimal.valueOf(calc.getTotalArea(new Rectangle(shape.height,shape.width))));
        System.out.println("getTotalArea adding 🧱: "+result);
      }
      if(shape instanceof Circle){
        result=result.add(BigDecimal.valueOf(calc.getTotalArea(new Circle(shape.radius))));
        System.out.println("getTotalArea adding 🔵: "+result);
      }
    }
    System.out.println("getTotalArea of all shapes result: "+result+"\n\n");

    String s = String.valueOf(result.doubleValue());
    System.out.println("s: "+s);
    System.out.println("s.indexOf(\".\"): "+s.indexOf("."));
    String threeDecimals = s.substring(0,s.length() - s.indexOf(".") >= 4 ? s.indexOf(".")+4 : s.length());
    double number = Double.parseDouble(threeDecimals);
    String decimalPart = threeDecimals.substring(threeDecimals.indexOf(".")+1);

    if(decimalPart.length() > 2 && Character.getNumericValue(threeDecimals.charAt(threeDecimals.length()-1)) > 5){
      number = number+0.01;
    }
    s = String.valueOf(number);
    String twoDecimals = s.substring(0,s.length() - s.indexOf(".") >= 3 ? s.indexOf(".")+3 : s.length());



    return Double.parseDouble(twoDecimals);

  }

}

class Shape{
  double base, height, side, width, radius;    
}

class Triangle extends Shape{
  Triangle(double base, double height){
    this.base=base;
    this.height=height;
  }
}

class Square extends Shape{
  Square(double side){
    this.side=side;
  }  
}

class Rectangle extends Shape{
  Rectangle(double height, double width){
    this.height=height;
    this.width=width;
  }
}

class Circle extends Shape{
  Circle(double radius){
    this.radius=radius;
  }
}

测试的结果是:

getTotalArea of square: 7439.73
getTotalArea of square: 7439.73
getTotalArea adding 🔲: 7439.73
getTotalArea of circle: 12722.91
getTotalArea adding 🔵: 20162.64
getTotalArea of all shapes result: 20162.64


s: 20162.64
s.indexOf("."): 5
res: 20162.64

所以看起来当有正方形和圆形的时候,我们在四舍五入方面有困难。

我们如何才能正确地四舍五入呢?

我们已经读过了。

java algorithm rounding
1个回答
0
投票

要四舍五入到小数点,请做Math.ceil(area * 100) 100; 这将以100为倍数,以确保你保留2个小数,然后除以返回它们的正确位置。

© www.soinside.com 2019 - 2024. All rights reserved.