Java 分式意大利面
第一次使用DecimalFormat类 我需要为我的值存储4位小数的精度。 让我向你展示我迄今为止所做的工作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
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实际上没有十进制类型。