Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Math.Round as 'int' not working

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

    Math.Round as 'int' not working

    Hi guys

    In Variables, I've declared:

    Code:
    private double mult = 2.0;
    private int var1 = 2
    private int var2 = 2
    then in OnBarUpdate, I've put:

    Code:
    Var1 = 3*Mult;
    Var2 = Math.Round(2*Mult, 0);
    The Var1 = 3*Mult works OK, but the Var2 = Math.Round(2*Mult, 0) doesn't compile: there's obviously a int/double mismatch. The exact error message is:

    Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)
    I need Var2 to be 'int' as I use it later in HMA(Median, Var2).

    Any advice as to how to solve this conundrum will be greatly appreciated.

    #2
    Originally posted by arbuthnot View Post
    Hi guys

    In Variables, I've declared:

    Code:
    private double mult = 2.0;
    private int var1 = 2
    private int var2 = 2
    then in OnBarUpdate, I've put:

    Code:
    Var1 = 3*Mult;
    Var2 = Math.Round(2*Mult, 0);
    The Var1 = 3*Mult works OK, but the Var2 = Math.Round(2*Mult, 0) doesn't compile: there's obviously a int/double mismatch. The exact error message is:

    I need Var2 to be 'int' as I use it later in HMA(Median, Var2).

    Any advice as to how to solve this conundrum will be greatly appreciated.
    Hi arbuthnot,

    Using the NinjaTrader editor, when you type in the following string...

    Var2 = Math.Round(

    When you enter the curved bracket, You will be offered a series of options to complete the statement. Just follow the format you want.

    RJay
    RJay
    NinjaTrader Ecosystem Vendor - Innovative Trading Solutions

    Comment


      #3
      hello arbuthnot,

      you could use the following code to load the integers with the doubles values:
      Code:
       
      Var1 = ([COLOR=blue]int[/COLOR])(3*Mult);
      Var2 = ([COLOR=blue]int[/COLOR])Math.Round(2*Mult,0);
      Marco

      Comment


        #4
        Hi arbuthnot,

        Using the NinjaTrader editor, when you type in the following string...

        Var2 = Math.Round(

        When you enter the curved bracket, You will be offered a series of options to complete the statement. Just follow the format you want.

        RJay
        Thanks very much, RJay. That's a very useful suggestion but it doesn't seem to work on my NT7. Maybe because I'm using XP...

        Comment


          #5
          hello arbuthnot,

          you could use the following code to load the integers with the doubles values:
          Code:
          Var1 = (int)(3*Mult); Var2 = (int)Math.Round(2*Mult,0);
          Marco
          Thanks very much, Marco, I've done as you suggested and that works perfectly. Not for the first time I'm very grateful to you.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          633 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          364 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          105 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          567 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          568 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X