Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 708733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:25:26+00:00 2026-05-14T04:25:26+00:00

I am seeing an intriguing situation rounding Currency in C# (VS 2008 SP1). Below

  • 0

I am seeing an intriguing situation rounding Currency in C# (VS 2008 SP1). Below is an image of the test cases:

alt text http://img697.imageshack.us/img697/8500/testcases.png

I was expecting cases five, six, and seven (my bad on not numbering them in the output) to round the number up to a penny.

Here is my test code:

static void Main(string[] args)
{
    decimal one = 10.994m;
    decimal two = 10.995m;
    decimal three = 1.009m;
    decimal four = 0.0044m;
    decimal five = 0.0045m;
    decimal six = 0.0046m;
    decimal seven = 0.0049m;
    decimal eight = 0.0050m;

    Console.WriteLine(one + ": " + one.ToString("C"));
    Console.WriteLine(two + ": " + two.ToString("C"));
    Console.WriteLine(three + ": " + three.ToString("C"));
    Console.WriteLine(four + ": " + four.ToString("C"));
    Console.WriteLine(five + ": " + five.ToString("C"));
    Console.WriteLine(six + ": " + six.ToString("C"));
    Console.WriteLine(seven + ": " + seven.ToString("C"));
    Console.WriteLine(eight + ": " + eight.ToString("C"));

    Console.ReadLine();
}

When I reflected into .ToString(string format) to see what was going on I found

public string ToString(string format)
{
    return Number.FormatDecimal(this, format, NumberFormatInfo.CurrentInfo);
}

which has the call to

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern string FormatDecimal(
    decimal value, 
    string format, 
    NumberFormatInfo info);

Is there some logic in that call that says the granularity for my current culture settings for NumberFormatInfo is two decimal places for currencty so don’t let the ten thousandths place roll the number up because it is insignificant?

How is this method implemented? Are we into bit shift land, or is something else going on?

Thanks for any insights.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:25:26+00:00Added an answer on May 14, 2026 at 4:25 am

    Following basic mathematical principles, cases 4, 5, 6, and 7 should not round up to a penny. You don’t round by starting at the right-most number and rounding up. You only look one digit to the right of the number you want to round to.

    http://www.enchantedlearning.com/math/rounding/

    the computer is just performing basic math, as they are supposed to.

    Edit – added

    a better link:
    http://math.about.com/od/arithmetic/a/Rounding.htm

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After seeing this image: http://2.bp.blogspot.com/_T-uXeKcGTnM/TIdoKBGwk9I/AAAAAAAABcs/CLW3_cRlN78/s1600/tumblr_kxovt0VLZy1qappj8.png I wonder is exists any tool for translating SQL
Seeing this: http://www.suckless.org/wiki.html . A wiki based on Mercurial. Are there any other non-code
I am seeing the below errors in my jsp page - javax.servlet.jsp.PageContext cannot be
seeing this blog post : http://mine.tuxfamily.org/?p=111 , I'm trying to disable the pivot flick
Seeing as Java doesn't have nullable types, nor does it have a TryParse(), how
Seeing various locking related question and (almost) always finding the 'loop because of spurious
Seeing lots of SOAP/RDS stuff, but just want to dump some MySQL data and/or
Seeing some strange things; help is being solicited. I have a query, like so:
Seeing as you can convert any document to a byte array and save it
After seeing the cool new reputation tab on the stackoverflow user page, I was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.