iwebie
HOME MOVIES TV SHOWS SPORTS GENERAL
 RSSPRIVACY
 

Java Rounding Decimal to 2 Places
Posted by admin on February 22nd, 2009

Watch over 100,000 movies and TV shows on your PC

Below is a Java Program containing a method round” to Round an Input Double Number to 2 Decimal Places. The same logic can be used for numbers of other Data Types. We can make use of 3 Built-in methods of the Math Function to achieve this.

[sourcecode language='java']
/*
public class JavaRoundingDemo {

private static double PI = 3.14159;

public static void main(String[] args) {

double piAfterRounding = round(PI, 2); //Rounding to 2 Decimal Places
System.out.println(”Simple Rounding Yeilds: ” + Math.round(PI));
System.out.println(”Rounded to 2 Decimal Places: ” + piAfterRounding);
}

public static double round(double input, int roundFactor) {
double p1 = (double) Math.pow(10, roundFactor); // Results in (100)
input = input * p1; // Results in 314.159
double temp = Math.round(input); // Results in 314.16
return temp / p1;
}
}
[/sourcecode]

Bookmark Article
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • Propeller

Leave a Reply

Most Popular

Watch over 100,000 movies and TV shows on your PC
Recent Posts

  • Watch Crazy on the Outside Online Free
  • Watch It’s Complicated Full Video Online
  • Watch Leap Year Free Stream Online
  • Watch Manchester United vs Birmingham Live Stream
  • Watch Avatar Online Stream
  • Watch Nine Online Video
  • Watch Crazy Heart Full Stream
  • Modern Family Season 1 Episode 11 | Watch Modern Family s01e11 Up All Night Full Episode
  • Burnley vs Arsenal EPL | Watch Burnley vs Arsenal Live Stream
  • Watch The Princess and the Frog Online Free
Archives

  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
Powered by WordPress