0

I am writing a code that will use numbers from an array and add them up. I only have doubles that have two decimals but i still get over 6 decimal numbers. How can i fix this? My code currently consists of;

 //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;
 }

the method will be activated upon a press of a JButton. The code for one of those buttons looks like this

  private void BtnFrisdrankActionPerformed(java.awt.event.ActionEvent evt) 
  {                                             
    double prijs = prijzen[0]; 
    TotaalPrijs(prijs);
  }
  • Why do you say you have 6 decimal numbers? How have you found 6? – Alexandre Fenyo Nov 02 '18 at 17:39
  • @AlexandreFenyo I didn't specificly mean six. But when I use this method it generally gives me, or a two decimal number(which I want). Or a number for example 11.1000000000000001. And I dont understand why this is the case – T.F.Berkers Nov 02 '18 at 17:56
  • I understand this question has been asked before and i was not clear in my title. But what i want to know is how do i fix it? I just want two decimals and not more. – T.F.Berkers Nov 02 '18 at 18:00
  • @T.F.Berkers You can use the `DecimalFormat()`. See https://stackoverflow.com/a/153785/479156. Like so: https://tio.run/##VY0xD4IwEIVn@ysuTBDjpboSN@PGIqNxqLRgkbaEHqgx/HYs0UGml3zvu3u1GMSmlvdp0qZ1HUEdAJJ6Eh5UoY1ojq4zglL23wdww5PrrdS2ypxUKWNtf210AUUjvIdMaAtvtvpBT4JCDE5LMKGKc@rC5fkCoqt8MpurxRzIEvZg1QMWOI44ch4lafDzlydl0PWEbXhGjY1lieXX47iFNXDcJbM7snGaPg – Ivar Nov 02 '18 at 18:08
  • Since you do not manipulate real numbers but only decimal numbers, and since you want exact arithmetic operations, use the BigDecimal class instead of the Double one or the double primitive type. This is because the double primitive type can not handle 0.1 correctly since it is not made of a sum of integer powers of 2 and integer powers of 0.5. Therefore, the double primitive type can only store approximate values for such a decimal number. See https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html – Alexandre Fenyo Nov 02 '18 at 18:40
  • If you want two decimal place either round the result or use a format which prints two decimal places. – Peter Lawrey Nov 02 '18 at 18:50
  • @Ivar I made a code that gives me the right amount of decimals when i use System.out.println() on it. Ben when i then use this to set a label this does not work. My code currently looks like this `public static double TotaalPrijs(double x) { totaal = totaal + x; System.out.println(df.format(totaal)); return totaal; }` and then the code for the button which will set the label is still the same. Thanks for the help – T.F.Berkers Nov 03 '18 at 14:42
  • @T.F.Berkers You only format what you are printing there. You will need to use that same code before you put it in the label. – Ivar Nov 03 '18 at 14:44
  • @Ivar i see how to do it now. Thanks again for the help! – T.F.Berkers Nov 03 '18 at 15:05

0 Answers0