根据输出要求,最后的结果是例如:1000.00  我定义为double为什么最后输出的是1000.0呢

解决方案 »

  1.   

    可以利用NumberFormat对输出结果进行自定义的格式控制,给你贴个代码最后保留小数点后两位
    import java.text.NumberFormat;
    import java.text.DecimalFormat;
    public class NumberFormatDemo {
        public static void main(String[] args){
            NumberFormat nf=new DecimalFormat(".00");
            double d=1500.0;
            String s=nf.format(d);
            System.out.println(s);
        }
    }
      

  2.   

    自己格式化一下
    for example
    double d = 1000.00;
    System.out.printf("%.2f\n", d);
    String s = String.format("%.2f", d);
    System.out.println(s);或者用 DecimalFormat 格式化
      

  3.   


    public static double round(double v, int scale) {
    if (scale < 0) {
    throw new IllegalArgumentException(
    "The   scale   must   be   a   positive   integer   or   zero");
    }
    BigDecimal b = new BigDecimal(Double.toString(v));
    BigDecimal one = new BigDecimal("1");
    return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }这个方法很好
      

  4.   

    不好意思,因为我不方便把源代码放上来,所以有些地方删了或改了,因为从来没学过编程,我只想想请教下如何能显示出来的是0.00这样的类型。谢谢
    public class Buyer
    {   private double money;
           
        public Buyer()
         {money = setBalance();
          System.out.println(toString());
        }
        
              
        private double setBalance()
        { 
          System.out.print("The balance has: " );
          return In.nextDouble();}
         
        public double money()
        { return money;}
         
        public String toString()
        { String abc = people();
          return abc;}
         
         public String people()
        { return " has " + money;}
    }
      

  5.   

    可以这样修改一下
    public String people()
      { return String.format(" has %.2f", money);}