Zohka
Zohka

Reputation: 13

Using BigDecimal as a beginner instead of Double?

I am currently learning Java and have stumbled upon the usage of "BigDecimal". From what I have seen, "BigDecimal" is more precise and therefore it is recommended to use it instead of the normal Double.

My question is: Should I always use "BigDecimal" over Double because it is always better or does it have some disadvantages? And is it worth it for a beginner to switch to "BigDecimal" or is it only recommended when have you more experience with Java?

Upvotes: 1

Views: 105

Answers (1)

Louis Wasserman
Louis Wasserman

Reputation: 198371

double should be used whenever you are working with real numbers where perfect precision is not required. Here are some common examples:

  • Computer graphics, for several reasons: exact precision is rarely required, as few monitors have more than a low four-digit number of pixels; additionally, most trigonometric functions are available only for float and double, and trigonometry is essential to most graphics work
  • Statistical analysis; metrics like mean and standard deviation are typically expected to have at most a little more precision than the individual data points
  • Randomness (e.g. Random.nextDouble()), where the point isn't a specific number of digits; the priority is a real number over some specific distribution
  • Machine learning, where multiplication factors are being learned and specific decimal precision isn't required

For values like money, using double at any point at all is a recipe for a bad time. BigDecimal should generally be used for money and anything else where you care about a specific number of decimal digits, but it has inferior performance and offers fewer mathematical operations.

Upvotes: 2

Related Questions