我正在编写一个代码,该代码将使用数组中的数字并将其相加。我只有两个有两位小数的双精度数,但我仍然得到超过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);
}