Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

stuck with Convert.ToInt32

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

    stuck with Convert.ToInt32

    Hi,

    Can anyone help me with this? I have no idea what the reason is for this. Apologies in advance if I am missing something obvious or fundamental.

    My problem is that the result for Convert.ToInt32(2.5) is 2 and not 3.

    I'll post a code snippet and the results it generates in the Output window.

    The code:

    Print("Calculating Plot positions, i: "+i+", i-Convert.ToInt32((double)i/4): "+(i-Convert.ToInt32((double)i/4)));
    Print("Calculating Plot positions, (double)i/4: "+((double)i/4));
    Print("Calculating Plot positions, Convert.ToInt32((double)i/4)): "+(Convert.ToInt32((double)i/4)));
    Print("Calculating Plot positions, 1.5: "+(1.5));
    Print("Calculating Plot positions, Convert.ToInt32(1.5)): "+(Convert.ToInt32(1.5)));
    Print("Calculating Plot positions, 2.5: "+(2.5));
    Print("Calculating Plot positions, Convert.ToInt32(2.5)): "+(Convert.ToInt32(2.5)));
    Print("Calculating Plot positions, 2.51: "+(2.51));
    Print("Calculating Plot positions, Convert.ToInt32(2.51)): "+(Convert.ToInt32(2.51)));
    Print("Calculating Plot positions, 3.5: "+(3.5));
    Print("Calculating Plot positions, Convert.ToInt32(3.5)): "+(Convert.ToInt32(3.5)));


    The results in the Output window:

    Calculating Plot positions, i: 10, i-Convert.ToInt32((double)i/4): 8
    Calculating Plot positions, (double)i/4: 2,5
    Calculating Plot positions, Convert.ToInt32((double)i/4)): 2
    Calculating Plot positions, 1.5: 1,5
    Calculating Plot positions, Convert.ToInt32(1.5)): 2
    Calculating Plot positions, 2.5: 2,5
    Calculating Plot positions, Convert.ToInt32(2.5)): 2
    Calculating Plot positions, 2.51: 2,51
    Calculating Plot positions, Convert.ToInt32(2.51)): 3
    Calculating Plot positions, 3.5: 3,5
    Calculating Plot positions, Convert.ToInt32(3.5)): 4


    Thank you in advance.
    Last edited by ktomi; 05-13-2009, 04:46 PM.

    #2
    Int in general will round down. You can use Math.Ceiling(##.##) to round up.

    Comment


      #3
      No, I don't want to round up. I want to round. And rounding 2.5 is 3, because from 0.5 we round up, that's the rule for the rounding. And in the example it works well for 1.5, 3.5, even 2.51 is rounded up, but not 2.5, and I still don't understand why.

      Comment


        #4
        Implicit Rounding

        This was interesting. By default the implicit rounding of int32 will round to the closest EVEN number.

        Specifies the strategy that mathematical rounding methods should use to round a number.

        Comment


          #5
          Yes, that link answers the question and it works. Thanks Sharad!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by GussJ, 03-04-2020, 03:11 PM
          16 responses
          3,279 views
          0 likes
          Last Post Leafcutter  
          Started by WHICKED, Today, 12:45 PM
          2 responses
          19 views
          0 likes
          Last Post WHICKED
          by WHICKED
           
          Started by Tim-c, Today, 02:10 PM
          1 response
          8 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by Taddypole, Today, 02:47 PM
          0 responses
          5 views
          0 likes
          Last Post Taddypole  
          Started by chbruno, 04-24-2024, 04:10 PM
          4 responses
          51 views
          0 likes
          Last Post chbruno
          by chbruno
           
          Working...
          X