Avatar of PearlJamFanatic
PearlJamFanatic
 asked on

restrict decimal places for double datatype

I have java code that uses double type. I am using a sdk util function to round and restrict the decimal to 2 place. something like:
openCurrentBar=Util.round(localDataSeries.getOpen(paramInt), 2);
highCurrentBar=Util.round(localDataSeries.getHigh(paramInt), 2);
lowCurrentBar=Util.round(localDataSeries.getLow(paramInt), 2);

Open in new window

It does fix the issue temporarily but later in the code these variables show value of 11 digit decimal places again. This happens when they are used for calculation. How can I restrict the decimal paces permanently to 2 digits only such that no matter what happens in the code it does not show more than 2 digits. openCurrentBar should be something like 2365.21 and NOT 2365.213456721309.
Java

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
SOLUTION
John Tsioumpris

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
PearlJamFanatic

ASKER
would that permanently restrict the decimal places to 2 places. What if this variable is reassigned a values later on in code. Would it again change back to 10 digits or so?
//initially
trailingAmount=priceAtBuy * 0.5/100;

//later in the code WILL THIS AGAIN SET IT BACK TO 12 DIGITS ??	
trailingAmount=priceAtBuy * trailingPercentage/100;	

Open in new window

CEHJ

If you go by my third and fourth sentences, that question becomes academic
PearlJamFanatic

ASKER
CEHG so what you suggest is that I use the values with all the decimals for my calculation and only at the time of display I use a rounding function like the one I am using now. Is that right? will this not make debugging rather difficult? I need to think about this. I use this code to create labels for display on the chart.
buyRisk=Util.round(buyPrice-stopLossPrice, 2);	
buyOpenedOnThisBar=paramInt;
Label txtLbl=new Label(new Coordinate(localDataSeries.getStartTime(paramInt), 
lowCurrentBar-localDataSeries.atr(paramInt, 10)*5), "Buy @ " +buyPrice+"\nRisk= "+buyRisk+" pts");		
addFigureFront(Plot.PRICE, txtLbl);	

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
CEHJ

so what you suggest is that I use the values with all the decimals for my calculation and only at the time of display I use a rounding function like the one I am using now. Is that right?

Yes. If you want 2 dp then

String displayVal = String.format("%.2f", doubleValue);

Open in new window

Actually that's a 'yes' with the qualification that you haven't restricted the actual value's precision at all. Only what the user sees
PearlJamFanatic

ASKER
Ok I have made the changes in my code. I use the Util.round() at the time of display. Why do you suggest converting to string for display? why not use the SDK provided function like Util.round() which takes double and returns double. Why should it matter as it is only for display?
CEHJ

Because strings are more predictable. And there's less likelihood of using the imprecise value in further calculations
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
PearlJamFanatic

ASKER
Thankyou
CEHJ

:)

BigDecimal is also good in certain circumstances for controlling precision, but there are gotchas