Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Round two decimal places 
 

In this section, you will learn about how to round the figure up to two decimal places in java.

 

Rounding off in Java - Round two decimal places

                         

In this section, you will learn about how to round the figure up to two decimal places in java. 

Round: This is the process of rounding with dollar amounts and provides the result in more than two decimal places after division. There are following methods to used in rounding the data:

Methods                  Data                  Rounded Data

Round Half Up         45.558              45.56
Round Half Down    45.555              45.55
Round Up                45.558              45.56
Round Down            45.558             45.55

Description of program:

The following program round two decimal places of the given number. It means numbers are rounded into the above methods and shows the number in two figures after decimal point. Here, apply the Round() method for rounding the given number and print the following output. 

Here is the code of program:

public class RoundTwoDecimalPlaces{
  public static void main(String[] args) {
    float num = 2.954165f;
    float round = Round(num,2);
    System.out.println("Rounded data: " + round);
  }

  public static float Round(float Rval, int Rpl) {
  float p = (float)Math.pow(10,Rpl);
  Rval = Rval * p;
  float tmp = Math.round(Rval);
  return (float)tmp/p;
    }
}

Download this example.

Output of program:

C:\vinod\Math_package>javac RoundTwoDecimalPlaces.java

C:\vinod\Math_package>java RoundTwoDecimalPlaces
Rounded data: 2.95

                         

» View all related tutorials
Related Tags: c com file ide class files stl date time variables static directory io make variable dates sed get ria this

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (
post your own) View All Comments Latest 10 Comments:

You sample helped me quickly. I don't feel like nor have the time to look through the Java API to find out where they've hidden this basic functionality.

Thanks

Posted by Charles on Friday, 09.5.08 @ 20:47pm | #77277

Why did you create your own rounding off routine when Java has its own rich rounding off scheme (see see the api for class "RoundingMode" at http://java.sun.com/javase/6/docs/api/)

Posted by Bob on Monday, 05.5.08 @ 12:08pm | #58522

It is very easy to understand

Posted by saravanamuthu on Thursday, 06.21.07 @ 11:45am | #19851

Good, Worth reading.

Posted by Rajesh on Thursday, 06.14.07 @ 19:42pm | #19226

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.