Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

BigDecmial vs double
 Post subject: BigDecmial vs double Post rating: 0   New post Posted: Fri 02 Apr, 2010, 23:50 

User rating: -
I'm receiving some abnormalities when performing arithmetic calculations on the currency prices.

Example:
1.53235 - 1.53165 = 7.0000000000014E-4

Should I be storing these prices in a BigDecimal variable?


 
 Post subject: Re: BigDecmial vs double Post rating: 0   New post Posted: Wed 07 Apr, 2010, 07:29 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Hi,
yes use a BigDecimal. Here is sample:
      BigDecimal ba = new BigDecimal(1.53235);
      BigDecimal bd = new BigDecimal(1.53165);
      ba = ba.setScale(5,BigDecimal.ROUND_HALF_UP);
      bd = bd.setScale(5,BigDecimal.ROUND_HALF_UP);
      System.out.println("result: "+ba.subtract(bd));


 
 Post subject: Re: BigDecmial vs double Post rating: 0   New post Posted: Thu 03 Jun, 2010, 20:16 

User rating: 0
Joined: Fri 07 May, 2010, 02:59
Posts: 61
Don't use BigDecimal. It's EXTREMELY slow. Your problem has nothing to do with needing to represent numbers in an extremely precise manner, it has to do with a lack of understanding for how to deal with floating point numbers.

Here's a simple example to illustrate things you can/should do instead if you want to get rid of long printout of a double, or other precision issues:

This one deals with rounding to avoid issues with price estimates:
// Some piece of code comes up with an estimate for a limit order price:
// double limitPrice = someFunction();

// this is what it looks like under the hood after
// someFunction() is finished
double limitPrice = 1.220200000000001;

// The following code attempts to submit an order with this price:
myOrder.setOpenPrice ( limitPrice );

// Some code attempts to submit the order:
engine.submitOrder ( "order label", myOrder );
// Throws a JFException because EUR/USD only allows 10ths of a pip, and the price
// calculated above is far too granular

// Instead:

double limitPrice = 1.220200000000001;

// Round limitPrice.  Note I used 10000.0 for the division; this is important.
limitPrice = ( (int) ( 10000 * limitPrice ) ) / 10000.0;

// Submit the order
myOrder.setOpenPrice ( limitPrice );
engine.submitOrder ( "order label", myOrder );
// no exception generated


This one deals with printing numbers in a more friendly fashion:
// This prints something similar to: The price is 1.4803030001e+2
System.out.println ( "The price is " + 148.0303000000001 );

// This prints: The price is 148.0303
System.out.format ( "The price is %7.4f\n", 148.0303000000001 );


-Brian


 

Jump to:  

  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com