Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Double to Int conversion

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Double to Int conversion

    Hello,

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

    When I compile this code
    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

    #2
    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

    Comment


      #3
      Hello,

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

      Code:
          {
              #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
      Attached Files

      Comment


        #4
        Resolution of double to int conversion

        Hi guys,

        I eventually found a solution as poking around :
        Code:
        			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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Renejm, Today, 06:17 AM
        0 responses
        3 views
        0 likes
        Last Post Renejm
        by Renejm
         
        Started by kazisl, Yesterday, 06:41 PM
        2 responses
        10 views
        0 likes
        Last Post NinjaTrader_PaulH  
        Started by impactX, 05-04-2021, 01:07 AM
        2 responses
        11 views
        0 likes
        Last Post NinjaTrader_Melissa  
        Started by op1031, Yesterday, 05:51 PM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_PaulH  
        Started by aaadetos, 05-04-2021, 09:28 AM
        3 responses
        14 views
        0 likes
        Last Post NinjaTrader_Heath  
        Working...
        X