java - 当我只使用两个十进制数时,为什么在相加时会得到大的十进制数,如何在 Java 中修复它?
问题描述
我正在编写一个代码,它将使用数组中的数字并将它们相加。我只有两位小数的双打,但我仍然得到超过 6 个小数。我怎样才能解决这个问题?我的代码目前包括:
//this is the array where I'll be getting the numbers from
public static double[] prijzen = new double[]{2.20,3.70,4.20,0.50,2.60,1.80};
//double where the numbers will be added and stored
public static double totaal = 0;
// a method that will calculate the total for me
public static double TotaalPrijs(double x)
{
totaal = totaal + x;
return totaal;
}
该方法将在按下 JButton 时激活。其中一个按钮的代码如下所示
private void BtnFrisdrankActionPerformed(java.awt.event.ActionEvent evt)
{
double prijs = prijzen[0];
TotaalPrijs(prijs);
}
解决方案
推荐阅读
- javascript - 如何比较时间戳,如果它们相同则只打印一个日期
- c++ - 如何快速将 HDC 位图复制到 3 维数组?
- spring - cvc-elt.1:找不到元素“beans”错误的声明
- php - WooCommerce 中特定产品类别的最小购物车商品数量
- sony-audio-control-api - 如何使用 Sony Control API 获取 Sony AVR 的“流信息”?
- javascript - 在 chrome 扩展中存储和检索复选框的值
- flask - 如何将模板导入到 Flask 中模板文件夹的子目录中?
- java - 如何修复“此 Web 容器尚未启动”+ javax.ejb.EJBException
- shell - 检索 DNS 响应
- reactjs - 如何在 React-Leaflet 中使用 getBounds() 函数