Restrict to 2 decimal places

Posted on 2002-04-03
Last Modified: 2008-03-10
I would appreciate advice as to how to limit the return from a calculation to 2 decimal places.



     Java Class for calculating and comparing the range and average of figures input by the user  

public class AverageRangeClass{
     private double sum, max, min;
     private int count;

     public class AverageRangeClass(){

     public void InputValue(double number){
          else if(number > max)
               else if(number < min)

     public double rangeToAverage(){
          return ((max-min)/(sum/count));


     Java program to check samples are
     maintained within a specified range,
     using KiloConv class, AverageRange class

public class SampleCheck{
     public static void main(String arg[]){
          double kilograms;
          int stns, lbs, i, numToSample;

          KiloConvClass kcc = new KiloConvClass();
          AverageRangeClass arc = new AverageRangeClass();
          SampleRulesClass src = new SampleRulesClass();

          System.out.println("How many samples are there?\n");
          for(i=1; i<=numToSample; i++){
          System.out.print("Please enter stones weight:");
          stns = Input.readInt();
          System.out.print("Please enter pounds weight:");
          lbs = Input.readInt();

          kilograms = kcc.ConvToKilo(stns,lbs);

          arc.InputValue (kilograms);
System.out.println("\nRange to Average = "+arc.rangeToAverage());

Question by:LittleRedHat
  • 2
  • 2

Accepted Solution

m_onkey_boy earned 300 total points
ID: 6916161


System.out.println("\nRange to Average = "+arc.rangeToAverage());


java.text.NumberFormat nf = java.text.NumberFormat.getNumberInstance();

System.out.println("\nRange to Average = " + nf.format(arc.rangeToAverage()));

Author Comment

ID: 6916381
Many thanks.  That's a great help.  The points are yours.

But please could I ask an extra favour ... if you have a few more moments to spare, I would appreciate if you could give me a quick/brief explanation of the code and how it works.  I'm vaguely following it, but would like to understand it.

With thanks again.


Expert Comment

ID: 6916427

java.text.NumberFormat is a class that formats numbers/time-date/currency.  

NumberFormat nf = NumberFormat.getNumberInstance() returns a NumberFormat object that knows how to format numbers based on the default locale of your machine/server.  If you want to format your number how a German may expect to see it, you can use:

NumberFormat nf = NumberFormat.getNumberInstance(new Locale("de", "DE"));

Anyway, the next step is to tell the NumberFormat how many digits past the decimal you want to display, as you may want different precision for different tasks.

This is done here:


Now, all you have to do is pass in a number you want to format and it will handle any rounding and formatting needed.

double dd = 4.5432340001f
String str = nf.format(dd);

Here str will be 4.54, in String form.

Note:  This can be done in reverse:

String strNumber = 4.54;
double dd = nf.parse(strNumber);


Author Comment

ID: 6916474
Thanks a million.  I really appreciate your taking the time and trouble to explain.  Your explanation has completely cleared the haze. :-)

Sorry I can't double the points :-(  but I'm almost out at the moment, having needed to post a few SOS's as I muddle through my first attempt at Java.

Hope you wont mind just double thanks instead.



Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 191
thymeleaf natural templating vs JSP 2 95
JUnit 4 @Before and @BeforeClass differences 3 59
Java - Why doesn't this JFrame work 3 58
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now