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 Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          648 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          369 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          108 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          572 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          574 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X