0
/*
Author: Noah Shaw 
Date: 9/2/2016
Purpose: Calculate the area + perimeter of a rectangle
*/
public class Rectangle {
public static void main (String [] args){
//Display the Area
System.out.print("The Area of the Rectangle is ");
System.out.println((7.9 * 4.5));

//Display the Perimeter
System.out.print("The Perimeter of the Rectangle is ");
System.out.println((4.5 + 7.9) * 2); 
}
}

The output is The Area of the Rectangle is 35.550000000000004 The Perimeter of the Rectangle is 24.8

My question is why is my rectangle area not just 35.55?

Thanks for your time, sorry I'm still new to Java.

progyammer
  • 1,498
  • 3
  • 17
  • 29
Noah
  • 41
  • 7
  • To format to 2 decimal places, there are many duplicates. See [this](http://stackoverflow.com/q/2808535/5743988), [this](http://stackoverflow.com/q/8819842/5743988), or [this](http://stackoverflow.com/q/153724/5743988) – 4castle Sep 04 '16 at 04:19
  • 1
    Standard `float` and `double` types cannot hold the number 7.9--the actual number will be 7.9000000000000003552713678800500929355621337890625 for a `double`. (They can hold 4.5, though.) Please see [this website](http://www.adambeneschan.com/How-Does-Floating-Point-Work/showfloat.php?floatvalue=7.9&floattype=double) for an explanation. – ajb Sep 04 '16 at 05:25

5 Answers5

2

By default java is giving you the highest precision possible because you did not assigned any datatype to your area.

If you want to get answer unto 2 decimal places you have to format console output. Below is the sample code of your question:

public class Rectangle {
public static void main (String [] args){
//Display the Area
double area=7.9 * 4.5;
System.out.printf("The Area of the Rectangle is %2.2f",area);

//Display the Perimeter
System.out.print("The Perimeter of the Rectangle is ");
System.out.println((4.5 + 7.9) * 2); 
}
}

Here printf is used instead of println and %2 is width of your answer and .2f is digits to use after decimal place.

1

if you work with floating point numbers you can work seamlessly with primitive as double data type, but about those numbers have to perform operations must be very careful because it may not go the right results.

Why? Because computers work in binary and when working with variables of type float or double precision make mistakes when working with values ​​that can not represent

How could it be otherwise, Java had already thought of this, and therefore provides BigDecimal, a class designed to solve these problems. But we still have a bad surprise in store.

The first thing to so one strip when using BigDecimal is the constructor BigDecimal (double), and this leads to the same problem. This constructor does is keep exactly the same representation as if working with a data type double. The solution is to work always with the constructor BigDecimal (String). BigDecimal's own documentation recommends it.

BigDecimal bd1 = new BigDecimal("8192.55");

in his case would be

BigDecimal bd = new BigDecimal(Float.toString((float) ((4.5 + 7.9) * 2))));
bd = bd.setScale(numberdecimals, BigDecimal.ROUND_HALF_UP); 
//in his case would be considering whether to keep the number of decimal when zero
System.out.println(bd);
Dev. Joel
  • 1,127
  • 1
  • 9
  • 14
0

The reason is double will have the actual value. So, what you actually want is to print only two fraction digits. So, your task is formatting. Here is how you do that:

import java.text.NumberFormat;


public class NumberFormating {
    public static void main(String[] args) {
        NumberFormat twoFractionDigitsNumberFormat = NumberFormat.getInstance();
        twoFractionDigitsNumberFormat.setMaximumFractionDigits(2);

        System.out.println(twoFractionDigitsNumberFormat.format(2.123456789));
    }
}
Multithreader
  • 878
  • 6
  • 14
0

The answer isn't just .55 because there are still decimals that have been calculated after it. Use this;

double area = Math.round(area*100.0)/100.0;
System.out.println(area);
Ross Keddy
  • 141
  • 2
  • 8
  • Sure this is a temporary solution or rounding to two decimal places, but it doesn't answer the question of why doubles are inaccurate. It's because floating point numbers aren't that precise. – Andrew Li Sep 04 '16 at 04:19
0

While finding area, you have two floating point numbers interacting with each other and floating point calculation is handle in a different way then integer calculation. In your case you have to format or cast the value to get two decimal points instead of a long string of decimals.

If you are interested in understanding how floating point calculation works, below I found two links which explain it:

http://floating-point-gui.de/basic/

http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

I hope that helps.

Bhavya Bansal
  • 257
  • 1
  • 15