0

I'm new to Java and I was wondering is there any way to get the total values for the area and circumfrence to round to one decimal place? It keeps outputting whole numbers and I don't want that.

package exercises;
import java.util.Scanner;
public class Exercises {
public static void main(String[] args) {
    Scanner user_input = new Scanner (System.in);
    System.out.println("Enter a value to represent the radius of a circle.");
    String radius = user_input.next();
    int radiusValue = Integer.parseInt(radius);

    int area = (int) (Math.PI * radiusValue * radiusValue);
    int circumference = (int) (2 * Math.PI * radiusValue);

    System.out.println("The area is: " + area + " and the circumference is: " + circumference);
  }

}
ernest
  • 49
  • 8

4 Answers4

0

You can't use the int data type for decimal numbers because int if is for integers. If you can deal with small precision errors you can use the floating point data type like float or double, otherwise you should use the BigDecimal class.

For the rounding part of the question you can see the answer to this question.

Juan
  • 5,525
  • 2
  • 15
  • 26
0
DecimalFormat formatter = new DecimalFormat("#.#");

Double area = (Math.PI * radiusValue * radiusValue);
Double circumference = (2 * Math.PI * radiusValue);

area = Double.parseDouble(formatter.format(area));
circumference = Double.parseDouble(formatter.format(formatter));

Integer are are whole numbers and does not allowed decimal.

DecimalFormat is class that allow your to parse and format numbers, the '#' indicate an integer or a digit,the '#' after decimal indicate the number of the decimal place you need in your answer, so in your case '#.#' would indicate one decimal place. To format your double you need to use format method from DecimalFormat, but results of format method are in String, so if you need them in double to use it as a number later then you need to parse it back to double.

jon
  • 1
  • 2
  • Whilst this may answer the question, it would be better if you could provide an explanation as to where the OP went wrong and what you did to resolve that. – achAmháin Feb 13 '18 at 21:44
0

As said previously, to store even single one decimal place, you HAVE TO use non-integer type (so float is perfect here). However, nobody says that you must to display all decimal places available - maybe this will give you some hints how to display your results in more pleasant form:

https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

Marek
  • 81
  • 1
  • 4
0

You need to change the data type to float or double depending on the precision you need in your calculations. Check the official documentation at https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html for more information on the data types available.

If you need the numbers rounded for a reason other than presentation, you can use Math.round(arg) like this:

Scanner userInput = new Scanner(System.in);
System.out.println("Enter a value to represent the radius of a circle.");
String radius = userInput.next();
float radiusValue = Float.parseFloat(radius);

float area = Math.round(((float) (Math.PI * radiusValue * radiusValue)) * 10F) / 10F;
float circumference = Math.round(((float) (2F * Math.PI * radiusValue)) * 10F) / 10F;

Math.round(arg) rounds its double or float argument to the nearest whole number, so by multiplying the original value by 10, rounding that and then dividing by 10, you end up with the original number rounded to one decimal place.

If it's just presentation that you need the precision for, check out the DecimalFormat class, as mentioned in another response to your question.

geco17
  • 5,152
  • 3
  • 21
  • 38