• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem/consult with precision of division with BigDecimals

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys

i have this situation

a and b 2 BigDecimals

a=14.00
b=6.00

for a simple calculator
a/b = 2.3333......
so for humans 14/6 = 2.33 good by infinite


for java 14/6 = 2.34 (a.divide(b,2))
but with (a.divide(b,3)) 2.33
why?

it should be the same?

regards
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason for the difference between 2.34 and 2.33 is that when you say a.divide(b,2)), that "2" is the rounding mode - which is BigDecimal.ROUND_CEILING. While "3" is BigDecimal.ROUND_FLOOR. In one case you're telling the computer to round up, and in the other you're telling it to round down. No surprise there. If you want it to round to whichever is closest, use BigDecimal.ROUND_HALF_DOWN or BigDecimal.ROUND_HALF_UP. The difference is very minor. You can examine the JavaDoc to learn the exact differences between rounding modes.

If you're asking why the result is 2.33 rather than 2.33333 or 2.3333333333333 or 2.333333333333333333333333333333333 or whatever, then the answer is that you need to tell it how many digits you want. If you want 10 digits after the decimal point, use a scale of 10:

a.divide(b,10, BigDecimal.ROUND_CEIL) = 2.3333333334
a.divide(b,10, BigDecimal.ROUND_FLOOR) = 2.3333333333

If you want more digits or less, use another number besides 10.

Since you originally did not specify the number of digits you wanted, BigDecimal assumed you wanted the same number as were used for the first BigDecimal in the calculation, a. Since you created this as 14.00, it had two digits after the decimal. That's what was used in your original answers, 2.34 and 2.33.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic