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 SnailHorn, Today, 10:49 PM
          0 responses
          7 views
          0 likes
          Last Post SnailHorn  
          Started by naanku, Today, 07:25 PM
          0 responses
          8 views
          0 likes
          Last Post naanku
          by naanku
           
          Started by milfocs, Today, 07:23 PM
          0 responses
          6 views
          0 likes
          Last Post milfocs
          by milfocs
           
          Started by PaulMohn, Today, 06:59 PM
          0 responses
          8 views
          0 likes
          Last Post PaulMohn  
          Started by bortz, 11-06-2023, 08:04 AM
          48 responses
          1,756 views
          0 likes
          Last Post carnitron  
          Working...
          X