Results 1 to 2 of 2
  1. #1
    marcosol is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default The precision and scale with the divide operation on a bigdecimal

    Hi
    I don't understand the scale with de BigDecimal.divide(), example below. Only if I give scale zero it returns 0.25.
    But I should give the precision of this method. Is the precision the same as the scale? How do I use the scale correctly?

    Java Code:
    public double getFraction(){
    		BigDecimal value1 = new BigDecimal("5000");
    		BigDecimal value2 = new BigDecimal("20000");
    		return value1.divide(value2,0).doubleValue();
    	}
    Thanks !

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: The precision and scale with the divide operation on a bigdecimal

    Normally you shouldn't set the scale value; a BigDecimal is a BigInteger multiplied by pow(10, -scale); the divide( ... ) method you're using sets the rounding mode for the division operator, not its scale (read teh API documentation for the BigDecimal class for details).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Divide and Conquer
    By whateverme in forum New To Java
    Replies: 1
    Last Post: 05-20-2011, 01:51 PM
  2. Divide Class Into 2 or more Classes
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 4
    Last Post: 03-29-2011, 12:19 AM
  3. Divide bigdecimal again
    By ellhar in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 10:19 AM
  4. All possible way to divide a number?
    By bobocheez in forum New To Java
    Replies: 4
    Last Post: 09-24-2010, 02:24 AM
  5. How to divide code in classes?
    By hendrix79 in forum New To Java
    Replies: 2
    Last Post: 12-10-2008, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •