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 Hwop38, 05-04-2026, 07:02 PM
          0 responses
          182 views
          0 likes
          Last Post Hwop38
          by Hwop38
           
          Started by CaptainJack, 04-24-2026, 11:07 PM
          0 responses
          334 views
          0 likes
          Last Post CaptainJack  
          Started by Mindset, 04-21-2026, 06:46 AM
          0 responses
          258 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by M4ndoo, 04-20-2026, 05:21 PM
          0 responses
          358 views
          0 likes
          Last Post M4ndoo
          by M4ndoo
           
          Started by M4ndoo, 04-19-2026, 05:54 PM
          0 responses
          188 views
          0 likes
          Last Post M4ndoo
          by M4ndoo
           
          Working...
          X