Sunday, February 8, 2015

How to round floating point number in Java

How to round floating point number in Java
--------------------------------------------
Rounding real in full:

Just use the method Math.floor () to round the real to the nearest integer:


Double a = Math. floor ( 1 . 99 ); //  1.0
Unlike the method Math.ceil () allows a real round to the nearest integer:


Double a = Math. ceil ( 1 . 01 ); //  2.0
If one wishes to respect the standard rounding rule, using the nearest higher or lower depending on the value around the decimal part, simply add 0.5 to the passed value Math.floor () :


Double a = Math. floor ( 1 . 99 + 0 . 5 ); //  2.0


Double a = Math. floor ( 1 . 49 + 0 . 5 ); //  1.0


If the value of the fraction is less than [b] 0.5 [/ b], the value will be rounded to the nearest whole number.
If the value of the fraction is greater than or equal to [b] 0.5 [/ b], the value will be rounded to the next whole number.
Warning: could also use a cast to type int or long to "lose" the fractional part, but it can cause problems with large values ​​because the cast can lead to a loss of information by truncation. Round to a real 'n' decimals: You can also make a rounded 'n' decimals using the following code:


 Select
Double a =  1 . 6,666,666 ;
//  Round  the  value  was  10 ^ -2
a * =  100 . 0 ;
a = . Math floor (a + 0 . 5 );
a / =  100 . 0 ; //  1.67
For simplicity, this method can be used directly

public  static  double  floor ( double a, int n) {
double p = Math. pow ( 10 . 0 , n)
return . Math floor ((a * p) + 0 . 5 ) / p;
 }
Which is used as follows:

Double a =  floor ( 1 . 6,666,666 , 2 ); //  1.67
But be careful, however, one can obtain an incorrect result when approaching the maximum limit doubles (1e308 anyway). For more accurate results, it may be towards the class BigDecimal and methods setScale () . Rounding to display: In contrast, if the result is to convert to a string, it is best to turn to the class java.text.DecimalFormat

How to use BigDecimal to add or subtract float and double in Java

How to use BigDecimal to add or subtract float and double in Java
-------------------------------------------------------------------
How to make precision calculations correct with floats or doubles?

If you try this:

 Select

. System.out println ( 1 . 3 - 1 . 2 );
You get: 0.10000000000000009 instead of 0.1, as shown on your calculator. In fact, it is impossible to accurately represent 0.1 or any negative power of 10 using a float or a double. The best solution for this problem is to use the java.math.BigDecimal class. For example, it is used like this:

 Select
BigDecimal bd1 =  new  BigDecimal ( " 1.3 " );
BigDecimal bd2 =  new  BigDecimal ( " 1.2 " );
. System.out println (bd1. substract (bd2));
Please note, you want to represent numbers must be passed as Strings in the constructor

J2EE Interview Question and answers for Java programmer

J2EE Interview Question and answers for Java programmer
----------------------------------------------------------
What is J2EE or JEE ?
What does Servlet JSP do in J2EE architecture ?
Difference between Application Server and Web Server?
What does EJB perovides EJB ?
What are popular application server which is used to deploy J2EE application?
What is advantage of using J2EE ?
What is the difference between Session Bean and Entity Bean?
What is distributed transaction and 2 phase commint?
What is difference in Java and J2EE ?
Does spring part of J2EE specificatoin ?

Jakarta Struts Interview Questions and Answers in Java

Jakarta Struts Interview Questions and Answers in Java
-------------------------------------------------------
What is struts ?
What is difference between Struts 1.0 and Struts 2.0?
What is Action class in Struts ?
What is difference between DispatchAction and LookupDispathAction in Struts ?
What is ActionServlet ?
Which design pattern is used in Struts framework?
What is difference between ForwardAction and IncludeAction in Struts?
What is difference between ActionForm and DynaActionForm in Struts ?
How can we prevent duplicate form submission using Struts?
What is difference between reset() and validate() in Struts framework?
What is struts-config.xml in Struts framework?

Resources to learn Java programming

Resources to learn Java programming
------------------------------------
Now, if you decide to learn Java programming, here are couple of resources, which you can take a look. I personally suggest picking a book, because they are almost always well written and catered for beginners. I personally recommend Head First Java to beginners, that's great book to start, if you don't know much about Java. You can also take a look at official Java tutorials offered by Sun Microsystems. These tutorials are comprehensive and covered almost all important details of Java programming language. Further you can use Google along with your learning. If you need to understand a particular concept, term or any issue with Java, Google can point you to specific resource. There are lots of blogs, tutorial sites, and free video tutorials on internet to learn Java programming.

Recommended Java Books for beginners
Head First Java
Thinking in Java

Recommend tutorials
Java SE tutorials
Javadoc for JDK 7