The need for bigdecimal
Download
1 / 8

The need for BigDecimal - PowerPoint PPT Presentation


  • 142 Views
  • Updated On :

The need for BigDecimal. Tim McKenna [email protected] Why BigDecimal?. BigDecimal provides exact decimal values doubles only approximate decimals use BigDecimal for money and business calculations yes, doubles are easier no, you cannot use doubles. BigDecimal vs double.

Related searches for The need for BigDecimal

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'The need for BigDecimal' - britney


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Why bigdecimal l.jpg
Why BigDecimal?

  • BigDecimal provides exact decimal values

  • doubles only approximate decimals

  • use BigDecimal for money and business calculations

  • yes, doubles are easier

  • no, you cannot use doubles


Bigdecimal vs double l.jpg
BigDecimal vs double

  • from an assignment that didn’t use BigDecimal…Type Vegan Survey Last Date RestaurantCode Y/N Amount Surveyed Name, LocationCF Y 3.59 2003-07-04 Blueberry Hill, YLMPlease enter survey amount (+ add, - subtract) > -3.59 Unable to subtract this amount -3.589999999999999857891452847979962825775146484375because there is only 3.589999999999999857891452847979962825775146484375 left!


If you don t believe me l.jpg
If you don't believe me…

  • from the IBM computer scientist who wrote the new, improved BigDecimal class: Using Java 5.0 BigDecimaland Decimal Arithmetic FAQ

  • Core Java Technologies Tech Tips:The need for BigDecimal


The bigdecimal class l.jpg
The BigDecimal Class

  • 98.7% of numeric data in business applications has a predetermined number of decimals:

    • currency, exchange rates, unit costs, discounts, taxes

    • precision: digits in number including decimals

    • scale: number of decimal places

  • JDBC maps DB decimal fields to BigDecimal

  • BigD class provides control of rounding behaviour

  • Examples: PaySchedule.java, PaySchedule2.java, DontUseDouble.java


Bigdecimal add subtract l.jpg
BigDecimal add & subtract

  • BigDecimal sum, difference, …; // note: immutable class

  • sum = addend.add(augend); // a = b + c

  • sum = sum.add(anotherBigDecimal); // a += d

  • difference = minuend.subtract(subtrahend); // a = b - c


Bigdecimal multply divide l.jpg
BigDecimal multply & divide

  • import static java.math.RoundingMode.HALF_UP;// standard rounding, import the constant

  • BigDecimal product, quotient; // immutable

  • product = multiplicand.multiply(factor);

  • product = // round result to 2 decimalsproduct.setScale(2, HALF_UP);

  • product = // multiply and roundmultiplicand.multiply(factor).setScale(2, HALF_UP);

  • quotient = // round result to 2 decimalsdividend.divide(divisor, 2, HALF_UP);


Bigdecimal comparisons l.jpg
BigDecimal comparisons

  • import static java.math.BigDecimal.ZERO;

  • payment.compareTo(interest) > 0"if payment is greater than interest "

  • principal.compareTo(payment) <= 0 "if principal is less than/equal to payment"

  • principal.compareTo(ZERO) == 0"if principal is equal to zero"

  • principal.equals(ZERO)…may not be what you mean. see API.


ad