BigDecimal for stock prices?

I understand what we should use BigDecimalfor all monetary values, but what about stock prices in dollars?

I noticed that the data feed API from major suppliers uses the type doublefor stock quotes. Does anyone know why?

Does this mean that my application can use the type doubleto store stock quotes that come from these suppliers?

+5
source share
5 answers

I work in this area. BigDecimal is obviously perfect in terms of accuracy, but it sucks in terms of performance. Doubling is an option in some circumstances (especially when dealing with normal stock prices, doubling easily — with appropriate precautions — can represent the entire price range of all stock exchanges that I regularly deal with).

Another option is if you know the DP range used by the exchanges in question, use a fixed point and normal int or long. To take an example, I know well that Xetra (German electronic exchange) currently has no more than 3 decimal places. Using 3dp, you can represent prices up to 2,147,483.647 with the usual int. Perfectly at an individual price, it is not profitable to represent general daily trading.

, , .

+7

, , ( ) - .

. , , - , , . - , BigDecimal .

+10

BigDecimal ( *). , ( , , mils ) , . "" .. Joda Money timeandmoney - .

BigDecimal , , IMOHO. (BigDecimal [ impl.], .)

.

* , .

+5

BigDecimal. , , , . " " , , , , .

, , , ...

+3

:

, API double . - , ?

API (, XML JSON), , - - , . double , ( ) API.

When you get the stock price as double and want to calculate it in a “monetary way” about it, make sure that you know how many decimal digits (after the period) it has (it must be not from a double, but from the type of stock price) and convert it to this scale in BigDecimal (or what you use to calculate money).

0
source

All Articles