Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Java
Expertise: Advanced
May 24, 2006



Application Security Testing: An Integral Part of DevOps

Use BigDecimal Instead of Double Data Type

In certain situations, you need to show the exact decimal values. If you use the double data type, you need to truncate the decimal values. To avoid truncation while obtaining proper decimal values, you can use BigDecimal instead of double.

Here's the code

public static void main(String arg[]){
   BigDecimal bd = new BigDecimal("0");	
   bd = bd.add(new BigDecimal("99.99"));
   bd = bd.add(new BigDecimal("1.99"));		
   System.out.println("BigDecimal result " + bd);
   double d = 99.99;
   d = d + 1.99;		
   System.out.println("Double result " + d);

BigDecimal result 101.98
Double result 101.97999999999999
Olinilavan Jayaraman
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date