-1

I am using this JAVA program where I am multiplying a double with a double and storing it in a double.

My expected output is 7.14 as per my mathematics knowledge, but I get 7.140000000000001. I don't understand why is the 0000000000001 part is coming

public class Main
{
    public static void main(String[] args) {
        double a=7.0;
        double b=1;
        double c=a*b*1.02;
        System.out.println(c);
    }
}
Akai
  • 1
  • Keep in mind that when you write `1.02`, the actual value you get is precisely `1.020000000000000017763568394002504646778106689453125` – harold Aug 20 '21 at 19:28

1 Answers1

-2

So you are getting a super high level precision output for your operation. If you want to set a precision limit, you can do so following the example below

double no=12.786;
DecimalFormat dec = new DecimalFormat("#0.00");
System.out.println(dec.format(no));
goudarziha
  • 327
  • 5
  • 20
  • 1
    While this is possibly what OP would want to do in the end, it's really important that they understand why it's necessary. And this answer does nothing to help with that. – Joachim Sauer Aug 20 '21 at 19:32