Home Java Java-bigdecimal Java Bigdecimal class example
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Java Bigdecimal class example

Advertisement
Java bigdecimal class comes under java.math library. Class bigdecimal enhances the users limitation over rounding action on numbers by providing a set of eight rounding modes such as ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, etc.

Java Bigdecimal class example

     

Java bigdecimal class comes under java.math library. Class bigdecimal enhances the users limitation over rounding action on numbers by providing a set of eight rounding modes such as ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, etc.

Calculations done by programs on integer, rational & decimal numbers using bigdecimal class, is performed through a technique known as arbitrary-precision process in which an undetermined number of digits of precision, are involved (precision of digits of the number is often confined to the host's system memory). 

BigDecimal class provides opportunity to perform several operations such as mathematical addition, subtraction, multiplication and division. 
By the availability of a set of eight rounding modes, user's control on applying rounding feature is increased. Others features like analyzing (comparison), format alteration (change), hashing and authorization on setting scales have increased the usage of this class in building Business applications. 

Java bigdecimal class objects can be used in holding decimal values of 16(sixteen) significant digits precisely. Example below shows how to get start working with bigdecimal class. In the code First a class object is created and than a String argument is passed. This String argument in double quotes inside the bigdecimal class constructor concludes the value of the object.

The bigdecimal class is designed to solve two types of problems that are associated with floating-point numbers. First, the bigdecimal class can be used to exactly represent decimal numbers. Second, it can be used to work with numbers that have more than 16 significant digits. If you haven't ever used this class, it's one that you should master and use for many business applications.

Java_Bigdecimal_Example.java

import java.lang.Math;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.*;

public class Java_Bigdecimal_Example {
  public static void main(String args[]) {

  NumberFormat currency = 
  NumberFormat.getCurrencyInstance(Locale.US);

  double apple_price = 32.86426;
  System.out.print("Math class round method :"
  + currency.format(Math.round(apple_price))
  "\n");

  // creating BigDecimal class object
  BigDecimal bigDecimal_Apple_price = 
  new BigDecimal("52.2516");
  System.out.print("java.math package BigDecimal class" +
  " methods example: \n"
  + currency.format((bigDecimal_Apple_price
  .setScale(BigDecimal.ROUND_HALF_UP))
  .floatValue()) "\n");

  System.out.print(currency.format((
  bigDecimal_Apple_price.setScale(0,
  BigDecimal.ROUND_HALF_EVEN)).floatValue()));
  }
}

Download the code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 21, 2008

Related Tutorials

Ask Questions?    Discuss: Java Bigdecimal class example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com