Announcement

Collapse
No announcement yet.

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 cmoran13, 04-16-2026, 01:02 PM
          0 responses
          43 views
          0 likes
          Last Post cmoran13  
          Started by PaulMohn, 04-10-2026, 11:11 AM
          0 responses
          25 views
          0 likes
          Last Post PaulMohn  
          Started by CarlTrading, 03-31-2026, 09:41 PM
          1 response
          162 views
          1 like
          Last Post NinjaTrader_ChelseaB  
          Started by CarlTrading, 04-01-2026, 02:41 AM
          0 responses
          98 views
          1 like
          Last Post CarlTrading  
          Started by CaptainJack, 03-31-2026, 11:44 PM
          0 responses
          158 views
          2 likes
          Last Post CaptainJack  
          Working...
          X