PDA

View Full Version : Double to Int conversion


grd974
03-13-2010, 01:15 PM
Hello,

I have a 101 question that I've wrestled with for a while now, without any success.

When I compile this code

private int Lst1BodySize = 0;
Lst1BodySize = ((Close[0]-Open[0])*10000);

I get this message "Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists - CS0266 - click for info" which doesn't help.

When Open=1.3620 and Close=1.3625, I would like to have 5 displayed in the output window instead of 4.99999999999945 !

Any help is welcome,
Gerard

Ralph
03-13-2010, 02:33 PM
You need to preceed your calculation with the explicit type-cast (int).
This type of conversion cuts your result down to the next valid integer. If that is not intended you could add a rounding function in addition.

Regards
Ralph

grd974
03-13-2010, 09:53 PM
Hello,

I tried this code but it does not work as my integer keeps the initial value.


{
#region Variables
private double bdsz = 99;
private int BodySize = 99;
#endregion

/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = false;
Overlay = false;
PriceTypeSupported = false;
BodySize = System.Convert.ToInt32(Math.Floor(bdsz));
}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
Print("The tick count of the current bar is " + Bars.TickCount.ToString());
bdsz = (Close[0]-Open[0])*10000;
Print("Close-Open is " + bdsz.ToString() + " pips");
Print(" Bodysize is " + BodySize.ToString() + " pips");
}


I would appreciate a resolution sample.
Gerard

grd974
03-13-2010, 10:06 PM
Hi guys,

I eventually found a solution as poking around :

Print("The tick count of the current bar is " + Bars.TickCount.ToString());
bdsz = (Close[0]-Open[0])*10000;
Print("Close-Open is " + bdsz.ToString() + " pips");
bdsz = bdsz + 0.5;
BodySize = (int) bdsz;
Print(" Bodysize is " + BodySize.ToString() + " pips");


Anyway, I welcome any smarter solution if you have one.

Gerard