Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Java BigDecimal divide examples 
 

In this example, Java bigdecimal class divide(BigDecimal divisor, int scale, int roundingMode) method working is demonstrated. Example contains scale feature which is an integer type value that defines number of digits after decimal.

 

Java BigDecimal divide examples

                         

In this example, Java bigdecimal class divide(BigDecimal divisor, int scale, int roundingMode) method working  is demonstrated. Example contains scale feature which is an integer type value that defines number of digits after decimal. Here the scale of the quotient will be specified in the program.  Also in the example, bigdecimal class ROUND_FLOOR  field is used that defines the Rounding mode to round towards negative infinity. Method throws Arithmetic Exception in following cases when the specified Rounding mode is ROUND_UNNECESSARY,
when the divisor value is zero and when the specified rounding mode does not represent a valid rounding mode.

With divisor value zero, the rounding mode becomes equal to ROUND_UNNECESSARY therefore method throws Arithmetic Exception , when the divisor value is zero.

Method also throws IllegalArgumentException in the case when the specified rounding mode is not a valid rounding mode.

Syntax for using the method:

public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)         

bigdecimal_objectName = 
bigdecimal_objectDividendName.bigdecimal.divide(bigdecimal_objectDivisorName, int scale, int roundingMode);

Java_BigDecimal_divide_int_scale_int_roundingMode.java

import java.math.BigDecimal;
import java.text.NumberFormat;

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

    System.out.println("Actual PI value with no " +
        "scale defined : " + (double22 7);

    int i = 22, j = 7;
    BigDecimal a = new BigDecimal(i);
    BigDecimal b = new BigDecimal(j),
    c = new BigDecimal(0);

    c = a.divide(b, 5, BigDecimal.ROUND_FLOOR);
    System.out.println("PI value with scale 5 : " 
        + c);

    BigDecimal m = new BigDecimal(i);
    BigDecimal n = new BigDecimal(j),
    o = new BigDecimal(0);

    // Creating NumberFormat class num object
    NumberFormat num = NumberFormat.getInstance();

    // Invoking .setMinimumFraction() in object num
    num.setMinimumFractionDigits(5);
    System.out.println("Formated  PI value : "
        + num.format(c));

    /*
     * Here an Arithmetic Exception is thrown by method
     
     * i = 22; j = 0; 
     * BigDecimal x = new BigDecimal(i);
     * BigDecimal y = new BigDecimal(j),
     * z = new BigDecimal(0);
     
     * z = x.divide(y, 4, BigDecimal.ROUND_FLOOR);
     * System.out.println("PI value with scale 4 : "
     *  + c); 
     */
  }
}

Download the source code

                         

» View all related tutorials
Related Tags: c class object objects integer method type default double decimal gd value return int if biginteger example work bigdecimal values

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

A useful example.

BTW 22 / 7 is a rational number. It is an approximation to the irrational number that represents the ratio of a circle's circumference to its diameter, generally known as pi.

Posted by anonymous on Thursday, 07.9.09 @ 19:22pm | #89200

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.