Java 如何设置双回程法的精度

Java 如何设置双回程法的精度,java,int,double,Java,Int,Double,下面是双返回型函数,我想得到精度为2个零的答案。我目前的答案是-4.5。而我希望它是-4.50。任何形式的帮助都将不胜感激 public double getAmount(){ double ftotal = car.hello(); return Math.round(ftotal * 100)/100.0d; } 参考以下代码- import java.text.DecimalFormat; public class Decimal2{ pri

下面是双返回型函数,我想得到精度为2个零的答案。我目前的答案是
-4.5
。而我希望它是
-4.50
。任何形式的帮助都将不胜感激

public double getAmount(){
        double ftotal = car.hello();

        return Math.round(ftotal * 100)/100.0d;
}
参考以下代码-

import java.text.DecimalFormat;

public class Decimal2{

    private static DecimalFormat df2 = new DecimalFormat(".##");

    public static void main(String[] args) {

        double input = 62.123454;
        System.out.println("double : " + df2.format(input));

    }
}

返回的值是双精度的,因此不能将0填充为双精度
因此,必须格式化返回值以创建字符串,如:

NumberFormat formatter = new DecimalFormat("#0.00");
String number = formatter.format(getTotal());     
System.out.println(number);
double
s是数字。数字上,-4.5和-4.50是完全相同的。末尾有零是一种表示方式(例如:格式化、字符串)。这就是说,
BigDecimal
有规模的概念。