【问题标题】:how to round the double values with 2 decimal points and store it in Vectors and display?如何用 2 个小数点舍入双精度值并将其存储在向量中并显示?
【发布时间】:2016-01-02 17:04:18
【问题描述】:
Vector <Double> v=new Vector<Double>();

for (double d=1;d<=50;d++) {
    v.add(d);
    v.add((double)Math.round((d*1.3)*100/100));
}

System.out.println(Math.round((5.255685650*1.3)*100/100));
System.out.println("Multiples of 1.3 is -->"+v);

我想要像 45.54 这样的小数,它也存储在向量中。 请告诉我..

【问题讨论】:

  • 那么究竟是什么问题?
  • 你想做什么?
  • 你为什么使用Vector?现在是 2016 年,而不是 1996 年。
  • 您的问题设置小数点后两位,已经回答here
  • 你的右括号放错了位置。试试Math.round((d*1.3)*100)/100 而不是Math.round((d*1.3)*100/100)

标签: java vector collections double rounding


【解决方案1】:

例如,如果您有一个小数点后五位的数字,您想将其转换为两位小数:

num=45.54345;
num*=100;  // multiply the number by 100 to get 4554.345
num = round(num); // round the number to 4554
num/=100;  // divide the number by 100 to get 45.54

【讨论】:

    【解决方案2】:

    请使用 DecimalFormat 类来格式化双精度值。请点击链接。

    [https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html][1]

    【讨论】:

      猜你喜欢
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多