Hi. I'm trying to figure out how to round floating point numbers with more flexibility than just rounding to integers or rounding to two decimal places.
Here is the class with my methods:
package samsExperiments; public class Shapes { public double circleCircumference(double radius) { double circumference = 2 * Math.PI * radius; return circumference; } public double circleArea(double radius) { double circleArea = Math.PI * radius * radius; return circleArea; } }
And here is the class that calls them:
package samsExperiments; import java.util.Scanner; import java.util.Arrays; import java.util.ArrayList; import java.util.Vector; import java.util.Hashtable; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; import java.util.Random; import static exercises.StaticMethodExample.*; import java.lang.StringBuilder; import java.text.DecimalFormat; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Map; import customExceptions.IntegerOutOfRangeException; public class SamsExperimentsMain { public static void main(String[] args){ Shapes shape = new Shapes(); double radius = 7.5; double circleArea = shape.circleArea(radius); double circleCircumference = shape.circleCircumference(radius); System.out.println("The area of the our circle is " + (Math.round(circleArea*100.0)/100.0) + " square inches, " + "and the perimeter is " + circleCircumference + " inches."); }//end of main method }//end of class
The output is:
The area of the our circle is 176.71 square inches, and the perimeter is 47.12388980384689 inches.
This is fine, but I want more control. For example, are there any round tools out there that look something like "round(circleCircumference,4)", which would give me 47.1238 for the circleCircumference variable in my output?