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.


 

Ads

Tutorials   
Java Bigdecimal Java bigdecimal toBigIntegerExact example Java bigdecimal shortValue example Java bigdecimal shortValueExact Java bigdecimal signum example Java bigdecimal stripTrailingZeros example Java bigdecimal subtract Java bigdecimal subtract method example Java bigdecimal toBigInteger example Java bigdecimal toEngineeringString example Java bigdecimal toPlainString example Java bigdecimal toString example Java bigdecimal ulp example Java bigdecimal unscaledValue example Java bigdecimal valueOf double example Java bigdecimal valueOf long example Java bigdecimal valueOf long method Java bigdecimal setScale method example Java bigdecimal setScale example Java bigdecimal scaleByPowerOfTen example Java bigdecimal scale example Java bigdecimal round example Java bigdecimal remainder method example Java bigdecimal remainder example Java bigdecimal precision example Java bigdecimal pow method example Java bigdecimal pow example Java bigdecimal plus method example Java bigdecimal plus example Java bigdecimal negate method example Java bigdecimal negate example Java bigdecimal multiply method example Java bigdecimal multiply example Java BigDecimal movePointRight example Java bigdecimal movePointLeft example Java bigdecimal min example Java bigdecimal max example Java BigDecimal divideAndRemainder example Java BigDecimal divideAndRemainder method example Java BigDecimal divide examples Java BigDecimal divideToIntegral example Java BigDecimal divideToIntegral method example Java BigDecimal doubleValue example Java BigDecimal equals example Java BigDecimal hashCode example Java BigDecimal intValue example Java BigDecimal intValueExact example Java BigDecimal longValue example Java BigDecimal longValueExact example Java BigDecimal abs example
Ads

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

Share on Google+Share on Google+

Java Bigdecimal class example

Posted on: November 21, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Java Bigdecimal class example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads