Thread: strange decimal rounding

  1. #1
    Registered User
    Join Date
    May 2006

    strange decimal rounding

    Hello everyone,

    As mentioned here,

    "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?

    using System;
    class Test
        static void Main()
            Decimal b = 100.9M;
            int a = (int)b;
            // output 100, other than 101

    thanks in advance,

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    100 is the closest integer in the direction of 0, as it should be.

    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
    Hannover, Germany
    How strange it is, right?
    I don't see any strangeness here. Your code does exactly as described.

    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