Addition of doubles in Java

Doubles are treated much the same way, but now you get to use decimal points in the numbers. This is a similar program that does addition and subtraction on doubles.

class AddDoubles {

  public static void main (String args[]) {

    double x = 7.5;
    double y = 5.4; 
    double z;


    System.out.println("x is " + x);
    System.out.println("y is " + y);
  
    z = x + y;
    System.out.println("x + y is " + z);
    
    z = x - y;
    System.out.println("x - y is " + z);

  }

 } 
 
Here's the result:
% javac AddDoubles.java
% java AddDoubles
x is 7.5
y is 5.4
x + y is 12.9
x - y is 2.0999999999999996

Previous | Next | Top
Last Modified May 18, 1999
Copyright 1997, 1999 Elliotte Rusty Harold
elharo@metalab.unc.edu