Point2D Double不存储双精度

时间:2018-09-19 17:24:09

标签: java floating-point double

所以我正在研究一个三角形类,我想使用Point2D.Double来存储高精度的点。

import java.awt.geom.Point2D.Double;

public class Triangle {

private Double pointOne = new Double();
private Double pointTwo = new Double();
private Double pointThree = new Double();
private final float PERCISION = 0.009f;

public Triangle(double x1, double y1, double x2, double y2, double x3, double y3)
{
    pointOne.x = x1;
    pointOne.y = y1;
    pointOne.setLocation(x2, y2);
    pointOne.setLocation(x3, y3);

}

public Double getPointOne() 
{
    return pointOne;
}

public Double getPointTwo() 
{
    return pointTwo;
}

public Double getPointThree() 
{
    return pointThree;
}

但是,当我在main中对其进行测试并键入以下代码时,它会打印出坐标,但精度非常低。我尝试使用浮点数,但始终以相同的结果结束。

    Triangle tri = new Triangle( 0.0000, 0.0000, 2.0008, 0.0000, 0.0000, 2.0000);

    System.out.println("Point 1 coordinates: (" + tri.getPointOne().getX() + ", " + tri.getPointOne().getY() + ")");
    System.out.println("Point 2 coordinates: (" + tri.getPointTwo().getX() + ", " + tri.getPointTwo().getY() + ")");
    System.out.println("Point 3 coordinates: (" + tri.getPointThree().getX() + ", " + tri.getPointThree().getY() + ")");

这里是打印出来的内容,以备您需要时使用。

点1坐标:(0.0,2.0)

点2坐标:(0.0,0.0)

第3点坐标:(0.0,0.0)

理想情况下,这就是我想要打印的内容。

点1坐标:(0.0000,2.0008)

第2点坐标:(0.0000,0.0000)

第3点坐标:(0.0000,0.0000)

1 个答案:

答案 0 :(得分:0)

用于自定义十进制格式

https://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html

编辑:我误解了您的问题!忘了我说的关于基元的内容,但是您可能需要执行一些格式化