Thread: strange decimal rounding

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    strange decimal rounding

    Hello everyone,


    As mentioned here,

    http://msdn.microsoft.com/en-us/libr...al(VS.80).aspx

    "Conversions from Decimal to other types are narrowing conversions that round the Decimal value to the nearest integer value toward zero."

    It conflicts my previous understanding that in all rounding in C#, we round to the nearest integer. Here is my test code. How strange it is, right?

    Code:
    using System;
    
    class Test
    {
        static void Main()
        {
            Decimal b = 100.9M;
            int a = (int)b;
    
            // output 100, other than 101
            Console.WriteLine(a);
    
            return;
        }
    }

    thanks in advance,
    George

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    100 is the closest integer in the direction of 0, as it should be.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    How strange it is, right?
    I don't see any strangeness here. Your code does exactly as described.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  2. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 02:14 AM
  3. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  4. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM