Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 分式意大利面_Java_Decimalformat - Fatal编程技术网

Java 分式意大利面

Java 分式意大利面,java,decimalformat,Java,Decimalformat,第一次使用DecimalFormat类 我需要为我的值存储4位小数的精度。 让我向你展示我迄今为止所做的工作 public class Paper { public String name, color, type,finish,grain; public double width,height,gsm, lbs,ppi; public DecimalFormat df; public Paper() { String patter

第一次使用DecimalFormat类

我需要为我的值存储4位小数的精度。 让我向你展示我迄今为止所做的工作

public class Paper 
{
    public String name, color, type,finish,grain;
    public double width,height,gsm, lbs,ppi;
    public DecimalFormat df;


    public Paper() 
    {
        String pattern = "#0.0000";
        df = new DecimalFormat(pattern);

    }

    public void setWidthAndHeight(String width, String height) 
    {

       //I've censured what I tried so far.    
      ///I need to ensure that the passed width has 4 decimals and that it is stored that way. 

       this.width = //ENTER answer here! :)

    }

感谢您的时间和耐心。

您可以使用BigDecimal来管理小数

BigDecimal n = new BigDecimal("100.12345");
n = n.setScale(4, RoundingMode.HALF_UP);
n = n.stripTrailingZeros();
System.out.println(n.toPlainString());
使用刻度将数字四舍五入到小数点后4位。使用stripTrailingZeros删除任何零,并使用toPlainString获取您的号码以便稍后打印


你的问题是什么?精确到四位小数?啊,对不起,我的错。Java实际上没有十进制类型。