Solved

Restrict to 2 decimal places

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

WTA

LRH

/*
     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(){
          sum=0;
          count=0;
          max=0;
          min=0;
     }

     public void InputValue(double number){
           sum+=number;
           count++;
          if(count==1){
               max=number;
               min=number;
          }    
          else if(number > max)
          max=number;
               else if(number < min)
               min=number;
     }

     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");
               numToSample=Input.readInt();
          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());

          }
     }
}
         
0
Comment
Question by:LittleRedHat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
m_onkey_boy earned 300 total points
ID: 6916161

Change:

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

To:

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


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

Author Comment

by:LittleRedHat
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.

LRH
0
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6916427
OK.  

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:

nf.setMaximumFractionDigits(2);

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);



0
 

Author Comment

by:LittleRedHat
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.

LRH

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Bot application - advice 3 79
hashmap order 17 59
Tomcat 9 + java 8 error while trying to deploy a war file 2 841
Java regex 5 17
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

738 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